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

相关推荐
武子康1 小时前
大数据-244 离线数仓 - Hive ODS 层建表与分区加载实战(DataX→HDFS→Hive)
大数据·后端·apache hive
武子康1 天前
大数据-243 离线数仓 - 实战电商核心交易增量导入(DataX - HDFS - Hive 分区
大数据·后端·apache hive
代码匠心3 天前
从零开始学Flink:Flink SQL四大Join解析
大数据·flink·flink sql·大数据处理
武子康4 天前
大数据-242 离线数仓 - DataX 实战:MySQL 全量/增量导入 HDFS + Hive 分区(离线数仓 ODS
大数据·后端·apache hive
SelectDB5 天前
易车 × Apache Doris:构建湖仓一体新架构,加速 AI 业务融合实践
大数据·agent·mcp
武子康5 天前
大数据-241 离线数仓 - 实战:电商核心交易数据模型与 MySQL 源表设计(订单/商品/品类/店铺/支付)
大数据·后端·mysql
IvanCodes5 天前
一、消息队列理论基础与Kafka架构价值解析
大数据·后端·kafka
武子康6 天前
大数据-240 离线数仓 - 广告业务 Hive ADS 实战:DataX 将 HDFS 分区表导出到 MySQL
大数据·后端·apache hive
字节跳动数据平台7 天前
5000 字技术向拆解 | 火山引擎多模态数据湖如何释放模思智能的算法生产力
大数据
武子康7 天前
大数据-239 离线数仓 - 广告业务实战:Flume 导入日志到 HDFS,并完成 Hive ODS/DWD 分层加载
大数据·后端·apache hive