计算最大值,通过最后的cleanup函数,计算把所有的数目通过map ,
最后的时候把最大的值放入cleanup函数中,通过这个函数返回。
------------------------------------------------------------------------------
private int maxNum = 0;
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] str = value.toString().split(",", 2);
try {// 对于非数字字符我们忽略掉
int temp = Integer.parseInt(str[0]);
if (temp > maxNum) {
maxNum = temp;
}
} catch (NumberFormatException e) {}
}
@Override
protected void cleanup(Context context) throws IOException,
InterruptedException {
context.write(new IntWritable(maxNum), new IntWritable(maxNum));
}
------------------------------------------------------------------------------
public void reduce(IntWritable key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
for (IntWritable val : values) {
if (val.get() > maxNum) {
maxNum = val.get();
}
}
}
@Override
protected void cleanup(Context context) throws IOException,
InterruptedException {
context.write(new IntWritable(maxNum), new IntWritable(maxNum));
}
reduce的数量只能有一个,而且逻辑也是相同的。。