其他分享
首页 > 其他分享> > Combiner

Combiner

作者:互联网

Combine合并

(1)Combiner是mr程序中Mapper和Reducer之外的一种组件。

(2)Combiner组件的父类就是Reducer。

(3)Combiner和reduce的区别在于运行的位置。

         Combiner是在每一个MapTask所在的节点运行。

         Reducer是接收全局所有Mapper的输出结果。

(4)Combiner的意义就是对每一个MapTask的输出进行局部汇总,以减小网络传输量。

(5)Combiner能够应用的前提是不能影响最终的业务逻辑,而且,Combiner的输出kv应该跟Reducer的输入kv类型要对应起来。

(6)Combiner 求和可以,求平均值不行。

 

 

自定义Combiner实现步骤

(1)自定义一个Combiner继承Reducer,重写Reduce方法

 1 public class WordCountCombiner extends Reducer<Text, IntWritable, Text, IntWritable> {
 2 
 3     private IntWritable outV = new IntWritable();
 4 
 5     @Override
 6     protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
 7 
 8         int sum = 0;
 9         for (IntWritable value : values) {
10             sum += value.get();
11         }
12      
13         outV.set(sum);
14      
15         context.write(key,outV);
16     }
17 }

 

(2)在Job驱动类中设置

job.setCombinerClass(WordCountCombiner.class);

 

标签:Combiner,IntWritable,自定义,outV,sum,Reducer
来源: https://www.cnblogs.com/xiao-wang-tong-xue/p/16512506.html