Hadoop MapReduce计算并显示Max值

时间:2013-11-29 23:24:10

标签: java hadoop mapreduce

如果我想编写一个“字数”程序来查找哪个字符的编号最大,我的Reducer类将是这样的:

private String maxWord;
private int max = 0; 

@Override
public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException 
{
    long sum = 0;
    for (LongWritable value : values) 
    {
        sum += value.get();
    }

    if(sum > max)
    {
        max = sum;
        maxWord.set(key);
    }
}

// only display the character which has the largest value
@Override
protected void cleanup(Context context) {
    context.write(new Text(maxWord), new LongWritable(max)));
}

但是在运行我的程序之后,它总是在Recuder Task中给出一个错误,即“NullPointerException”,我不明白为什么。我如何改进我的计划以实现这一目标?

2 个答案:

答案 0 :(得分:2)

您应该尝试在setup()方法中初始化实例变量。在我的头顶之前,你甚至可以在初始化字符串之前调用String.set()吗?那样做。

如评论中所述,堆栈跟踪也会有所帮助。

答案 1 :(得分:2)

就是这样:

  public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
  int max =0;
  Text maxWord = new Text();

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

            if(sum > max)
            {
                max = sum;
                maxWord.set(key);
            }

  }

  @Override
  protected void cleanup(Context context) throws IOException, InterruptedException {
      context.write(maxWord, new IntWritable(max));
  }
}