【问题标题】:map reduce word count example doesn't work地图减少字数示例不起作用
【发布时间】:2017-09-03 15:39:59
【问题描述】:

我尝试自己实现字数统计示例,这是我的映射器实现:

public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {

    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        Text word = new Text();     
        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()) {
            word.set(tokenizer.nextToken());
            context.write(word, new IntWritable(1));
        }
    }
}

和减速器:

public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
    public void reduce(Text key, Iterator<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;
        while (values.hasNext())
            sum += values.next().get();
    context.write(key, new IntWritable(sum));
    }
}

但我执行此代码得到的输出看起来只是 mapper 的输出,例如,如果输入是“hello world hello”,则输出将是

hello 1
hello 1
world 1

我还在映射和归约之间使用组合器。谁能解释一下这段代码有什么问题?

非常感谢!

【问题讨论】:

    标签: mapreduce word-count


    【解决方案1】:

    用这个替换你的 reduce 方法:

            @Override
            protected void reduce(Text key, java.lang.Iterable<IntWritable> values, org.apache.hadoop.mapreduce.Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException,
                    InterruptedException {
                int sum = 0;
                for (IntWritable value : values) {
                    sum += value.get();
                }
                context.write(key, new IntWritable(sum));
            }
    

    所以底线是您没有覆盖正确的方法。 @Override 有助于解决此类错误。

    还要确保将 Reduce.class 设置为 reduce 类而不是 Reducer.class!

    ;) 高温高压 约翰内斯

    【讨论】:

    • 谢谢。我被这个问题困扰了一两天。
    【解决方案2】:

    如果您不想在覆盖时使用 reduce 方法的参数,则替代解决方案可以是:

    @Override
    protected void reduce(Object key, Iterable values, Context context) throws 
    IOException, InterruptedException {
    
     int sum = 0;
     Iterable<IntWritable> v = values;
     Iterator<IntWritable> itr = v.iterator();
    
     while(itr.hasNext()){
        sum += itr.next().get();
     }
    
     context.write(key, new IntWritable(sum));
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-30
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多