
上QQ阅读APP看书,第一时间看更新
Mapper program
This is the Mapper part of the main program that generates Key Value pairs of the input split provided by Record Reader. It extends the Hadoop Mapper class, specifying the types of data that will be used while processing. The first two types, Object and Text, are required when reading the text file. The line number offset will become the key and the actual text will become the value. The last two types are an intermediate result generated by Mapper. The text type will be the key and LongWritable will become the value of the generated key-value pairs. Following is the complete code for the Mapper class:
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class UtilityMapper extends Mapper<Object, Text, Text, LongWritable> {
// Initialize key and value part of the pair
private Text utility = new Text();
private LongWritable amount = new LongWritable(1);
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
// Mapper receive line by line of the text file
String row = value.toString();
// Check whether the text line contains data or its empty
if (!row.isEmpty()) {
// Split the data based in <space> deliminator
String[] rowValues = row.split(" ");
// set name of the utility
utility.set(rowValues[0]);
// set amount paid for that utility
amount.set(Long.parseLong(rowValues[2]));
// assign Utility as Key and Amount as value in keyValue pair
context.write(utility, amount);
}
}
}