我一直在ClouderaVM4.7中使用Hadoop2.0。我正试着打印出这本书中最神秘的5个单词 cleanup
文档中描述如何使用的方法。但根本没人打电话。
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
方法和Map器中的。
1条答案
按热度按时间8yoxcaq71#
你需要添加
@Override
对您的cleanup
方法。另外,如果您使用的是旧的api,则必须检查mapper接口是否扩展了
Closable
接口-定义close方法(而不是cleanup,后者是新MapReduceAPIMap器的方法)