地图减少字数示例不起作用
我尝试自己实现字数统计示例,这是我的映射器实现:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的减少方法替换为以下方法:
所以底线是您没有覆盖正确的方法。 @Override 可以帮助解决此类错误。
还要确保将Reduce.class设置为reduce类而不是Reducer.class!
;)
华泰
约翰内斯
Replace you reduce method with this one:
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
如果您不想在重写时使用减少方法的参数,则替代解决方案可以是:
If you don't want to play with args of reduce method while overriding than alternate solution can be: