Java – Hadoop MapReduce counts and displays the maximum value

Hadoop MapReduce counts and displays the maximum value… here is a solution to the problem.

Hadoop MapReduce counts and displays the maximum value

If I wanted to write a “word count” program to find which character has the largest number, my Reducer class would look like this:

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)));
}

But after running my program, it always gives me an error in Recuder Task i.e. “NullPointerException” and I don’t understand why. How can I improve my plan to achieve this goal?

Solution

You should try to initialize your instance variable in the setup() method. In my head, can you even call String.set() before initializing the string? So that’s it.

As mentioned in the review, stack traces can also help.

Related Problems and Solutions