hadoop使用intwritable减少输出总是在2处停止

g9icjywg  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(338)

reduce程序总是将值输出为2,即使给定键的值列表大于2。
例如:字数测试文件的字数与字数测试文件的字数相似,字数测试文件的字数与字数测试文件的字数相似
输出为:this 2 the 2 word 2

reduce代码是:

public class WordCountReducer
  extends Reducer<Text, IntWritable, Text, IntWritable> {
    //public static final log LOG = LogFactory.getLog(MyMapper.class);
  @Override
  public void reduce(Text key, Iterable<IntWritable> values,
      Context context)
      throws IOException, InterruptedException {
      IntWritable count = null;

      for (IntWritable value: values) {
           if (count == null) {
            count = value;
           } else {

            count.set(count.get() + value.get());

           }
          }

    context.write(key, count);
  }

}

你能解释一下这个问题吗?当我使用int counter时,它工作得很好。

elcex8rz

elcex8rz1#

count = value;

别这样。reducer重用这个可写的,因此,不管您将它设置为什么,它最终都将成为该键的值列表中的最后一个值。
相反,这样做。

count = new IntWritable();

相关问题