5.MapReduce之Combiner-预聚合

目录

概述

在 MR、Spark、Flink 中,常用的减少网络传输的手段。

通常在 Reducer 端合并,shuffle 的数据量比在 Mapper 端要大,根据业务情况及数据量极大时,将大幅度降低效率;且预聚合这种方式也是有其缺点,不能改变业务最终的逻辑,否则会出现,计算结果不正确的情况。

本地预计算 Combiner 意义

如下图,可以清晰看出,预聚合和在 Reducer 端合并的数据量差距,数据量小时,作用不明显,当接近 TB 级时,就非常不一样了。

实践

前提

注意:前提是不能改变最终的业务逻辑。下面是一个求平均数的例子

举例:

求平均数

3,5,7 --> 15/3 = 5

2,6 -->8/2=4

(5+4)/2=4.5

(3+5+7+2+6)/5=4.6

最终结果不对

代码

注意:这里的代码为了区分,Combiner 是单独写成一个类,实际使用中,直接使用 Reducer 实现,就可以了。官方的单词统计,就是这样使用的。可以对比一下。

java 复制代码
public class WordCountCombiner {

    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 WordCountCombinerExample 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 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(WordCountCombiner.class);
        // 注意此
        job.setCombinerClass(WordCountCombinerExample.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);
    }
}

日志观察

注意:观察下面注释的说明信息,预聚合和未预聚合的区别就比较明显,易观察到了。

bash 复制代码
Map-Reduce Framework
		Map input records=3
		Map output records=5
		Map output bytes=52
		Map output materialized bytes=46
		Input split bytes=113
		# 此处就是 Combine
		# 注销此处, recoreds =0 job.setCombinerClass(WordCountCombinerExample.class);
		Combine input records=5
		Combine output records=3
# 对比		
Map-Reduce Framework
		Map input records=3
		Map output records=5
		Map output bytes=52
		Map output materialized bytes=68
		Input split bytes=113
		Combine input records=0
		Combine output records=0

结束

至此,MapReduce之Combiner-预合并 就结束了,如有疑问,欢迎评论区留言。

相关推荐
武子康8 小时前
大数据-237 离线数仓 - Hive 广告业务实战:ODS→DWD 事件解析、广告明细与转化分析落地
大数据·后端·apache hive
大大大大晴天10 小时前
Flink生产问题排障-Kryo serializer scala extensions are not available
大数据·flink
武子康2 天前
大数据-236 离线数仓 - 会员指标验证、DataX 导出与广告业务 ODS/DWD/ADS 全流程
大数据·后端·apache hive
武子康3 天前
大数据-235 离线数仓 - 实战:Flume+HDFS+Hive 搭建 ODS/DWD/DWS/ADS 会员分析链路
大数据·后端·apache hive
DianSan_ERP4 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
够快云库4 天前
能源行业非结构化数据治理实战:从数据沼泽到智能资产
大数据·人工智能·机器学习·企业文件安全
AI周红伟4 天前
周红伟:智能体全栈构建实操:OpenClaw部署+Agent Skills+Seedance+RAG从入门到实战
大数据·人工智能·大模型·智能体
B站计算机毕业设计超人4 天前
计算机毕业设计Django+Vue.js高考推荐系统 高考可视化 大数据毕业设计(源码+LW文档+PPT+详细讲解)
大数据·vue.js·hadoop·django·毕业设计·课程设计·推荐算法
计算机程序猿学长4 天前
大数据毕业设计-基于django的音乐网站数据分析管理系统的设计与实现(源码+LW+部署文档+全bao+远程调试+代码讲解等)
大数据·django·课程设计
B站计算机毕业设计超人4 天前
计算机毕业设计Django+Vue.js音乐推荐系统 音乐可视化 大数据毕业设计 (源码+文档+PPT+讲解)
大数据·vue.js·hadoop·python·spark·django·课程设计