1
我一直在使用Cloudera VM 4.7中的Hadoop 2.0。我試圖在描述如何使用cleanup
方法中打印出5個最常見的單詞。但它根本不會被調用。清理不在減速器中運行
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
方法和映射器。
我嘗試過,但我得到的錯誤'類型WordCount.Reduce的方法清理(Reducer.Context)必須覆蓋或實現超類型方法' – Joren 2014-12-03 11:43:30
@Joren我已經修復了答案。 – 2014-12-03 13:35:37