以下是使用MapReduce编程实现Top10操作的map与reduce方法中的一段代码,即提取最大的10个数,并正序(升序)输出。请填写空缺处的代码,运算符左右两侧不需要空格,比如a+b,而不是a + b;同时要注意单词大小写。
时间: 2024-02-11 21:07:44 浏览: 20
map输出键值对:(NullWritable, IntWritable)
```
public static class Top10Mapper extends Mapper<LongWritable, Text, NullWritable, IntWritable> {
private TreeSet<Integer> top10 = new TreeSet<Integer>();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
Integer num = Integer.parseInt(line);
top10.add(num);
if (top10.size() > 10) {
top10.remove(top10.first());
}
}
protected void cleanup(Context context) throws IOException, InterruptedException {
for (Integer num : top10) {
context.write(NullWritable.get(), new IntWritable(num));
}
}
}
public static class Top10Reducer extends Reducer<NullWritable, IntWritable, NullWritable, IntWritable> {
private TreeSet<Integer> top10 = new TreeSet<Integer>();
public void reduce(NullWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
for (IntWritable value : values) {
top10.add(value.get());
if (top10.size() > 10) {
top10.remove(top10.first());
}
}
for (Integer num : top10) {
context.write(NullWritable.get(), new IntWritable(num));
}
}
}
```
reduce输出键值对:(NullWritable, IntWritable)
阅读全文