【问题标题】:Hadoop Reduce output using IntWritable always stops at 2Hadoop使用IntWritable减少输出总是停在2
【发布时间】:2014-03-31 16:29:33
【问题描述】:

Reduce 程序始终将值输出为 2,即使给定键的值列表大于 2。

例如: 字数测试文件有像 字数测试文件有像 字数测试文件有类似

输出是: 这 2 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 它工作正常。

【问题讨论】:

    标签: java hadoop reduce


    【解决方案1】:
    count = value;
    

    不要这样做。 reducer 会重用这个可写对象,因此,无论您将其设置为什么,它最终都会成为该键的值列表中的最后一个值。

    相反,这样做。

    count = new IntWritable();
    

    【讨论】:

    • 感谢您的回复。这看起来是一个新功能,并没有找到更多细节。您能否更详细地解释一下 Reduce 循环?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 2018-04-01
    • 1970-01-01
    相关资源
    最近更新 更多