3.MapReduce实践-单词统计

目录

概述

官网文档速递

MapReduce :分布式计算框架

通常情况下,一个 MR 作业是有 2 个部分构成:MapTask ReduceTask(可以没有)

MapReduce 核心进程

主要有三个:

  • MapTask
  • ReduceTask
  • MRAppMaster :负责整个 MR 作业的调度/协调工作

MapReduce 编程规范

与其说编程规范,编程模板更容易理解。

官方文档速递

  • MapReduce 作业的编程模板
    • 自定义 Mapper
      • extends Mapper<Object, Text, Text, IntWritable>
        • Object, Text : Mapper 的输入数据 Key 的类型,输入数据 Value 的类型
        • Text, IntWritable :Mapper 的输出数据 Key 的类型,输出数据 Value 的类型
      • 重写 public void map 方法:实现自己的业务逻辑
    • 自定义 Reducer
      • extends Reducer<Text,IntWritable,Text,IntWritable>
        • Text,IntWritable :Reducer 的输入数据 Key 的类型,输入数据 Value 的类型
        • 重写 public void reduce 实现自己的业务逻辑
    • 自定义 Driver
      • 通过 Driver 将整个 MR 作业串起来
        • 获取 Job
        • 设置作业的 Class
        • 设置自定义的 Mapper 类
        • 设置自定义的 Reducer 类
        • 设置自定义的 Mapper 类输出的 Key Value 类型
        • 设置自定义的 Reducer 类输出的 Key Value 类型
        • 设置作业的输入数据目录
        • 设置作业的输出数据目录
        • 作业的提交

单词统计案例

java 复制代码
public class WordCount {

    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            String str = value.toString();
            String[] split = str.split(",");
            IntWritable ONE = new IntWritable(1);
            for (String word : split) {
                context.write(new Text(word), ONE);
            }
        }
    }

    public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int count = 0;
            for (IntWritable value : values) {
                count = count + value.get();
            }
            context.write(key, new IntWritable(count));
        }
    }


    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration configuration = new Configuration();
		// 可以是文件夹,那么会统计文件夹下所有的文件
        String sourcePath = "data/wc.data";
        String distPath = "downloadOut/wc-out.data";

        FileUtil.deleteIfExist(configuration, distPath);

        Job job = Job.getInstance(configuration, "word count");
        job.setJarByClass(WordCount.class);
        job.setCombinerClass(WordCountReducer.class);
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job, new Path(sourcePath));
        FileOutputFormat.setOutputPath(job, new Path(distPath));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
java 复制代码
public static void deleteIfExist(Configuration configuration, String distPath) throws IOException {
    FileSystem fileSystem = FileSystem.get(configuration);
    if (fileSystem.exists(new Path(distPath))) {
        fileSystem.delete(new Path(distPath), true);
    }
    fileSystem.close();
}

源码

结束

至此 MapReduce实践-单词统计 就结束了,如有疑问,欢迎评论区留言。

相关推荐
学术小白人5 分钟前
JPCS出版| 往届检索可查 | 第四届机械工程与先进制造智能化技术研讨会(MEAMIT 2026)
大数据·人工智能·搜索引擎·能源·制造·ei会议·rdlink研发家
冬至喵喵12 分钟前
FLINK故障重启策略
大数据·python·flink
元智启14 分钟前
企业AI智能体:智能体经济崛起,重构产业价值坐标系——从单点赋能到生态重构的产业革命
大数据·人工智能·重构
好大哥呀25 分钟前
Hadoop yarn
大数据·hadoop·分布式
Ydwlcloud29 分钟前
AWS国际版新账号注册隐藏优惠全解析:2026年实测避坑指南
大数据·服务器·人工智能·云计算·aws
————A31 分钟前
从 RAG 走不通开始:设备运维场景下的一次诊断系统重构思考
大数据·数据库·人工智能
Elastic 中国社区官方博客35 分钟前
Elasticsearch:2025年的企业搜索 - 是否需要进行抓取?
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
Dxy123931021638 分钟前
ES批量写入数据:从兼容旧版到适配ES8的最佳实践
大数据·elasticsearch
成长之路51441 分钟前
【工具变量】国地税合并DID数据(2009-2023年)
大数据
痕忆丶44 分钟前
Git_Rebase_Conflict_Resolution
大数据·git