【发布时间】:2017-03-27 11:07:44
【问题描述】:
我正在尝试学习 mapreduce。从MapReduce WordCount 中所示的 WordCount 示例开始,当我在 eclipse 中执行代码时,它的输出是正确的字数。 I/p 文件内容如下:-
你好世界再见世界
它的输出是
再见 1
你好 1
世界 2
之后,我通过用逗号替换输入文件中每个单词后的空格来测试代码。
现在我已将输入恢复为与以前相同,但现在输出中的 WordCount 是预期结果的两倍。
再见 2
你好 2
世界 4
我的代码如下:
public static class TokenizerMapper extends Mapper<Object, Text, Text,IntWritable>{
public static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()){
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{
int sum=0;
for(IntWritable val:values){
sum +=val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] str) throws Exception{
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(str[0]));
FileOutputFormat.setOutputPath(job,new Path(str[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
任何人都可以解释一下Reducer Method中每个单词的值是如何分组的,因为它正在对特定单词的每个值进行求和。它正在检查同一个单词是否有两个计数。
谢谢
【问题讨论】:
-
删除同一输入文件夹中的tmp文件:)
-
@vefthym 是的,你是对的。创建了一个 tmp 文件。你能告诉我为什么创建这个 tmp 文件吗?
标签: hadoop mapreduce word-count