2014-12-03 277 views
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方法和映射器。

回答

0

您需要将@Override注释添加到您的cleanup方法中。

此外,如果使用的是一个旧的API时,必须检查是否映射器接口扩展了Closable接口 - 它定义了close方法(而不是清除这对新的MapReduce API映射器的方法)

@Override 
public void close() { 

} 
+0

我尝试过,但我得到的错误'类型WordCount.Reduce的方法清理(Reducer.Context)必须覆盖或实现超类型方法' – Joren 2014-12-03 11:43:30

+0

@Joren我已经修复了答案。 – 2014-12-03 13:35:37