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实践-单词统计 就结束了,如有疑问,欢迎评论区留言。

相关推荐
物联网软硬件开发-轨物科技13 小时前
【轨物方案】新能源的下半场:构筑光伏场站全生命周期智慧运维新范式
大数据·人工智能·物联网
汇智信科21 小时前
智慧矿山和工业大数据解决方案“智能设备管理系统”
大数据·人工智能·工业大数据·智能矿山·汇智信科·智能设备管理系统
阿里云大数据AI技术21 小时前
Hologres Dynamic Table 在淘天价格力的业务实践
大数据·人工智能·阿里云·hologres·增量刷新
OpenCSG1 天前
新能源汽车行业经典案例 — 某新能源汽车 × OpenCSG
大数据·人工智能·汽车·客户案例·opencsg
外参财观1 天前
流量变现的边界:携程金融按下暂停键后的冷思考
大数据·人工智能·金融
CCPC不拿奖不改名1 天前
两种完整的 Git 分支协作流程
大数据·人工智能·git·python·elasticsearch·搜索引擎·自然语言处理
智在碧得1 天前
碧服打造DataOps全链路闭环,定义大数据工程化发布新标杆
大数据·网络·数据库
亿信华辰软件1 天前
构建智慧数据中台,赋能饮料集团全链路数字化转型新引擎
大数据·人工智能·云计算
Elastic 中国社区官方博客1 天前
使用瑞士风格哈希表实现更快的 ES|QL 统计
大数据·数据结构·sql·elasticsearch·搜索引擎·全文检索·散列表
isNotNullX1 天前
什么是数字脱敏?一文讲透数字脱敏概念
大数据·数据安全·数据可视化·数字脱敏