【发布时间】:2015-12-15 12:47:46
【问题描述】:
映射器:
Mapper类是一个泛型类型,有四个形式类型参数,分别指定map函数的输入键、输入值、输出键和输出值类型
public class MaxTemperatureMapper
extends Mapper<LongWritable, Text, Text, IntWritable> {
private static final int MISSING = 9999;
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String year = line.substring(15, 19);
int airTemperature;
if (line.charAt(87) == '+') { // parseInt doesn't like leading plus signs
airTemperature = Integer.parseInt(line.substring(88, 92));
} else {
airTemperature = Integer.parseInt(line.substring(87, 92));
}
String quality = line.substring(92, 93);
if (airTemperature != MISSING && quality.matches("[01459]")) {
context.write(new Text(year), new IntWritable(airTemperature));
}
}
减速机:
四个形式类型参数用于指定输入和输出类型,这 减少功能的时间。 reduce函数的输入类型必须匹配map函数的输出类型:Text和IntWritable
public class MaxTemperatureReducer
extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int maxValue = Integer.MIN_VALUE;
for (IntWritable value : values) {
maxValue = Math.max(maxValue, value.get());
}
context.write(key, new IntWritable(maxValue));
}
}
但在此示例中,从未使用过密钥。
Mapper中的key有什么用,完全没用过?
为什么 key 是 LongWritable 的?
【问题讨论】: