Rohit Haritash
Rohit Haritash

Reputation: 404

Map Reduce - Not able to get the rigth key

Hi I am writing map reduce code finding the maximum temperature. The problem is that I am getting the maximum temperature but without the corresponding key.

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

Text year=new Text();

int maxTemperature=Integer.MIN_VALUE;
    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        for(IntWritable valTemp:values) {
            maxTemperature=Math.max(maxTemperature, valTemp.get());
        }

        //System.out.println("The maximunm temperature is " + maxTemperature);
        context.write(year, new IntWritable(maxTemperature));
    }
}

mapper imagin like

1955  52
1958  7
1985  22
1999  32

and so on.

It is overwriting the keys and printing all the data. I want only maximum temperature and its year.

Upvotes: 1

Views: 226

Answers (1)

Chris White
Chris White

Reputation: 30089

I see a couple of things wrong with your code sample:

  1. Reset the maxTemperature inside the reduce method (as the first statement), at the moment you have a bug in that it will output the maximum temperature seen for all preceding key/values

  2. Where are you configuring the contents of year? in fact you don't need to, just call context.write(key, new IntWritable(maxTemperature); as the input key is the year

  3. You might want to create a IntWritable instance variable and re-use it rather than creating a new IntWritable when writing out the output value (this is an efficiency thing rather than a potential cause of your problem)

Upvotes: 1

Related Questions