统计过程中对每一个maptask的输出进行局部汇总,以减小网络传输量即采用Combiner功能

1、分析

黑猴子的家:MapReduce WordCount 局部汇总(Combiner)

2、方案一

1)增加一个WordcountCombiner类继承Reducer

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WordcountCombiner extends Reducer<Text, IntWritable, Text, IntWritable>{

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values,
            Context context) throws IOException, InterruptedException {

        int count = 0;
        for(IntWritable v :values){
            count += v.get();
        }
        
        context.write(key, new IntWritable(count));
    }
}

2)在WordcountDriver驱动类中指定combiner

//指定需要使用combiner,以及用哪个类作为combiner的逻辑
job.setCombinerClass(WordcountCombiner.class);

3、方案二

1)将WordcountReducer作为combiner在WordcountDriver驱动类中指定

//指定需要使用combiner,以及用哪个类作为combiner的逻辑
job.setCombinerClass(WordcountReducer.class);

2)运行程序

黑猴子的家:MapReduce WordCount 局部汇总(Combiner)

4、基于wordcount案例

https://www.jianshu.com/p/c8726f1ccd8f

5、Code -> GitHub

https://github.com/liufengji/hadoop_mapreduce.git

相关文章:

  • 2021-08-26
  • 2021-10-20
  • 2021-04-01
  • 2021-12-30
  • 2022-01-09
  • 2021-12-07
  • 2022-01-19
  • 2021-12-19
猜你喜欢
  • 2021-08-12
  • 2021-04-30
  • 2021-04-24
  • 2021-11-03
  • 2021-12-20
  • 2021-08-30
  • 2021-07-17
相关资源
相似解决方案