地图减少字数示例不起作用

发布于 2024-10-27 16:41:19 字数 1130 浏览 2 评论 0原文

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

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));
    }
}

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

hello 1
hello 1
world 1

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

多谢!

I try to implement the word count example by myself, here's my implementation of the mapper:

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));
        }
    }
}

and reducer:

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));
    }
}

but the output I get for executing this code looks like the output of mapper only, for example, if the input is "hello world hello", the output would be

hello 1
hello 1
world 1

I also use combiner between mapping and reducing. Can anyone explain me what's wrong with this code?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转瞬即逝 2024-11-03 16:41:19

将您的减少方法替换为以下方法:

        @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!

;)
华泰
约翰内斯

Replace you reduce method with this one:

        @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));
        }

So bottom line is you're not overriding the correct method. The @Override helps with this kind of errors.

Also make sure you set Reduce.class as reduce class and not Reducer.class !

;)
HTH
Johannes

纵情客 2024-11-03 16:41:19

如果您不想在重写时使用减少方法的参数,则替代解决方案可以是:

@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));
}

If you don't want to play with args of reduce method while overriding than alternate solution can be:

@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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文