Big Data Architect’s Handbook
上QQ阅读APP看书,第一时间看更新

Reducer program

This class extends the Reducer class, which contains generic types. To extend it, we need the types to be defined in a similar way to the process required in the Mapper class. We set up our Mapper to produce <Text,LongWritable> values that will be used as input in the Reducer class. These two types will become the first two values while extending the Reducer class. The last two values define the type of the final output key-value pairs. In our case, it will also be <Text,LongWritable>, as we have to produce a result that calculates the total amount against each bill paid. Following is the complete code for the Reducer class:

import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class UtilityReducer extends Reducer<Text,LongWritable,Text,LongWritable> {
private LongWritable result = new LongWritable();

public void reduce(Text key, Iterable<LongWritable> values, Context context)
throws IOException, InterruptedException {

// Initiate variable total
long total = 0;

// Go through all the values for a specific key and sum them up
// to achieve our desired output.
for (LongWritable val : values) {
total += val.get();
}

// Assign calculated value
result.set(total);

// Assign the Utility as Key and the summed amount as value
context.write(key, result);
}
}