概念
每一个 map 都可能会产生大量的本地输出,Combiner 的作用就是对 map 端的输出先做一次合并,以减少在 map 和 reduce 节点之间的数据传输量,以提高网络IO 性能,是 MapReduce 的一种优化手段之一
特点总结:
1. Combiner是MR程序中Mapper和Reduce之外的⼀种组件
2. Combiner组件的⽗类就是Reducer
3. Combiner和Reducer之间的区别在于运⾏的位置
4. Reduce阶段的Reducer是每⼀个接收全局的Map Task 所输出的结果
5. Combiner是在合并排序后运⾏的。因此map端和reduce端都可以调⽤此函数。
6. Combiner的存在就是提⾼当前⽹络IO传输的性能,是MapReduce的⼀种优化⼿段。
7. Combiner在驱动类中的设置:
job.setCombinerClass(MyCombiner.class);
注意:combiner不適合做求平均值这类需求,很可能就影响了结果
实现步骤
在这里以单词统计为例,实现Combiner
1、自定义一个 combiner 继承 Reducer,重写 reduce 方法
public class MyCombiner extends Reducer<Text,LongWritable,Text,LongWritable> {
/*
key : hello
values: <1,1,1,1>
*/
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
long count = 0;
//1:遍历集合,将集合中的数字相加,得到 V3
for (LongWritable value : values) {
count += value.get();
}
//2:将K3和V3写入上下文中
context.write(key, new LongWritable(count));
}
}
2、在 job 中设置 job.setCombinerClass(CustomCombiner.class)
job.setCombinerClass(MyCombiner.class);
combiner 能够应用的前提是不能影响最终的业务逻辑,而且,combiner 的输出 kv 应该跟 reducer 的输入 kv 类型要对应起来
3、对使用Combiner之前和之后的日志进行对比

通过对比发现,使用Combiner之后,Reduce输入的键值对数量降低了,提供了网络传输效率。
本文介绍了MapReduce中Combiner组件的概念与作用,详细解释了其如何通过减少map和reduce节点间的网络传输量来提升性能,并给出了一个具体的单词计数实例。
1734

被折叠的 条评论
为什么被折叠?



