【发布时间】:2015-09-25 22:17:30
【问题描述】:
我的 Mapper 任务返回以下输出:
2 c
2 g
3 a
3 b
6 r
我已经编写了生成正确输出的 reducer 代码和 keycomarator,但是我如何获得 Mapper 输出的前 3 个(按计数前 N 个):
public static class WLReducer2 extends
Reducer<IntWritable, Text, Text, IntWritable> {
@Override
protected void reduce(IntWritable key, Iterable<Text> values,
Context context) throws IOException, InterruptedException {
for (Text x : values) {
context.write(new Text(x), key);
}
};
}
public static class KeyComparator extends WritableComparator {
protected KeyComparator() {
super(IntWritable.class, true);
}
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
// TODO Auto-generated method stub
// Logger.error("--------------------------> writing Keycompare data = ----------->");
IntWritable ip1 = (IntWritable) w1;
IntWritable ip2 = (IntWritable) w2;
int cmp = -1 * ip1.compareTo(ip2);
return cmp;
}
}
这是reducer的输出:
r 6
b 3
a 3
g 2
c 2
reducer 的预期输出是计数前 3 位,即:
r 6
b 3
a 3
【问题讨论】: