Mapreduce 映射减少字数示例不';行不通

Mapreduce 映射减少字数示例不';行不通,mapreduce,word-count,Mapreduce,Word Count,我尝试自己实现单词计数示例,下面是我对映射器的实现: 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 Te

我尝试自己实现单词计数示例,下面是我对映射器的实现:

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));
        }
    }
}
我还在映射和归约之间使用组合器。谁能解释一下这个代码有什么问题吗


非常感谢

将您的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));
        }
@覆盖
受保护的void reduce(文本键、java.lang.Iterable值、org.apache.hadoop.mapreduce.Reducer.Context)抛出IOException,
中断异常{
整数和=0;
for(可写入值:值){
sum+=value.get();
}
write(key,newintwriteable(sum));
}
所以底线是您没有覆盖正确的方法。@Override有助于解决此类错误

还要确保将Reduce.class设置为Reduce class,而不是Reducer.class

)() 嗯
Johannes

用以下方法替换您的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));
        }
@覆盖
受保护的void reduce(文本键、java.lang.Iterable值、org.apache.hadoop.mapreduce.Reducer.Context)抛出IOException,
中断异常{
整数和=0;
for(可写入值:值){
sum+=value.get();
}
write(key,newintwriteable(sum));
}
所以底线是您没有覆盖正确的方法。@Override有助于解决此类错误

还要确保将Reduce.class设置为Reduce class,而不是Reducer.class

)() 嗯
Johannes

如果您不想在重写时使用reduce方法的args,那么替代解决方案可以是:

@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));
}
@覆盖
受保护的void reduce(对象键、Iterable值、上下文)抛出
IOException,InterruptedException{
整数和=0;
Iterable v=数值;
迭代器itr=v.Iterator();
while(itr.hasNext()){
sum+=itr.next().get();
}
write(key,newintwriteable(sum));
}

如果您不想在重写时使用reduce方法的args,那么替代解决方案可以是:

@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));
}
@覆盖
受保护的void reduce(对象键、Iterable值、上下文)抛出
IOException,InterruptedException{
整数和=0;
Iterable v=数值;
迭代器itr=v.Iterator();
while(itr.hasNext()){
sum+=itr.next().get();
}
write(key,newintwriteable(sum));
}

谢谢您的帮助。我被这个问题困扰了一两天。谢谢你。我在这个问题上纠缠了一两天。