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

相关推荐
涛思数据(TDengine)9 小时前
通过最严时序标准,再登产业图谱榜首,TDengine 时序数据库在可信数据库大会荣获双荣誉
大数据·数据库·时序数据库·tdengine·涛思数据
All In丶9 小时前
Tdengine 时序库年月日小时分组汇总问题
大数据·时序数据库·tdengine
涛思数据(TDengine)9 小时前
新客户 | TDengine 时序数据库是怎么在钢厂“撬动”PI 的?
大数据·运维·数据库·时序数据库·tdengine
Giser探索家9 小时前
什么是2米分辨率卫星影像数据?
大数据·人工智能·数码相机·算法·分类·云计算
芯希望9 小时前
芯伯乐XBL6019 60V/5A DC-DC升压芯片的优质选择
大数据·人工智能·物联网·dc-dc·电子元器件·电源管理ic·xblw芯伯乐
青云交13 小时前
Java 大视界 -- 基于 Java 的大数据分布式计算在气象灾害数值模拟与预警中的应用(388)
java·大数据·flink·分布式计算·预警系统·数值模拟·气象灾害
蓝倾97614 小时前
唯品会以图搜图(拍立淘)API接口调用指南详解
java·大数据·前端·数据库·开放api接口
lifallen15 小时前
HBase的异步WAL性能优化:RingBuffer的奥秘
大数据·数据库·分布式·算法·性能优化·apache·hbase
WLJT12312312317 小时前
冷冻食材,鲜美生活的新选择
大数据