Hadoop WordCount 组合器

Hadoop WordCount Combiner

https://hadoop.apache.org/docs/stable/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html#Source_Code

在字数统计示例中,reduce 函数同时用作合并器和缩减器。

   public static class IntSumReducer extends Reducer<Text, IntWritable, Text,IntWritable> {

      public void reduce(Text key, Iterable<IntWritable> values, Context context) 
    throws IOException, InterruptedException {
       int sum = 0;
       for (IntWritable val : values) {
           sum += val.get();
       }
       context.write(key, new IntWritable(sum));
   }
  }

我理解 reducer 的工作方式,但是对于组合器,假设我的输入是

  <Java,1> <Virtual,1> <Machine,1> <Java,1>

它考虑第一个 kv 对并给出相同的输出...!!??因为我只有一个值。它怎么会考虑两个键并使

  <Java,1,1>  

因为我们一次只考虑一对 kv? 我知道这是一个错误的假设;有人请纠正我这个问题

Combiner 在发送到 reducer 之前先合并 mapper 结果。

主机上的映射器可能会输出许多相同的键值对。组合器将

在发送到 reducer 之前先合并 map 输出,从而减少

mapper 和 reducer 之间的洗牌成本。

所以如果一个mapper输出为(key, 1) (key, 1),combiner会合并 结果为 (key ,[1,1])

Combiner 运行s 在地图输出上。 在您的情况下,地图输出就像

<Java,1> <Virtual,1> <Machine,1> <Java,1>,

所以它会 运行 每个键,所以在你的情况下 Java 出现了两次,因此它生成的结果是 (Key, [Comma separated Values]).

IntSumReducer class继承了Reducer class如果我们查看 documentation

,Reducer class 在这里施展魔法

"Reduces a set of intermediate values which share a key to a smaller set of values. Reducer implementations can access the Configuration for the job via the JobContext.getConfiguration() method.

Reducer has 3 primary phases:

Shuffle:The Reducer copies the sorted output from each Mapper using HTTP across the network.

Sort:The framework merge sorts Reducer inputs by keys (since different Mappers may have output the same key).

The shuffle and sort phases occur simultaneously i.e. while outputs are being fetched they are merged."

程序调用相同的 class 进行组合和归约操作;

job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);

所以我发现如果我们只使用 一个数据节点 我们不一定要调用组合器 class 对于这个 wordcount 程序 因为减速器 class 本身负责组合器工作。

job.setMapperClass(TokenizerMapper.class);
job.setReducerClass(IntSumReducer.class);

如果只使用一个数据节点,上述方法对wordcount程序也有同样的效果。