我一直在使用Cloudera VM 4.7中的Hadoop 2.0。我试图在cleanup
方法中打印5个最常出现的单词documentation描述如何使用。但它根本没有被召唤。
public static class Reduce extends MapReduceBase implements
Reducer<Text, IntWritable, Text, IntWritable> {
private java.util.Map<String, Integer> top5 = new HashMap<String, Integer>(5);
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
reporter.getCounter(statistics.UNIQUE_TERMS).increment(1);
if (sum < 5) {
reporter.getCounter(statistics.LT5_TERM).increment(1);
}
if (this.top5.size() < 5) {
top5.put(key.toString(), sum);
} else {
for (Entry<String, Integer> e : this.top5.entrySet()) {
if (sum > e.getValue()) {
this.top5.remove(e.getKey());
this.top5.put(key.toString(), sum);
break;
}
}
}
output.collect(key, new IntWritable(sum));
}
protected void cleanup(org.apache.hadoop.mapreduce.Reducer.Context context) throws IOException, InterruptedException {
System.out.println(this.top5);
}
}
如何使方法按预期运行?
修改:此问题也适用于setup
方法和映射器。
答案 0 :(得分:0)
您需要将@Override
注释添加到cleanup
方法。
此外,如果您使用的是旧API,则必须检查Mapper接口是否扩展了Closable
接口 - 它定义了close方法(而不是cleanup,这是新mapreduce API映射器的方法)
@Override
public void close() {
}