Mapreduce WordCount 示例给出错误的输出
Mapreduce WordCount Example giving wrong output
我正在尝试学习 mapreduce。从 MapReduce WordCount 中所示的 WordCount 示例开始时,当我在 Eclipse 中执行代码时,它的输出是正确的字数统计。 I/p 文件内容如下:-
Hello World Bye World
它的输出是
Bye 1
Hello 1
World 2
之后,我通过将输入文件中每个单词后的 space 替换为逗号来测试代码。
现在我已将输入还原为与以前相同,但现在输出中的 WordCount 是预期结果的两倍。
Bye 2
Hello 2
World 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 中每个词的值是如何分组的,因为它对特定的每个值求和 word.Where 它检查同一个词是否有两个计数。
谢谢
必须将输入文件夹作为输入路径,其中必须有两个内容相同的文件,这可能是重复计数的原因
我正在尝试学习 mapreduce。从 MapReduce WordCount 中所示的 WordCount 示例开始时,当我在 Eclipse 中执行代码时,它的输出是正确的字数统计。 I/p 文件内容如下:-
Hello World Bye World
它的输出是
Bye 1
Hello 1
World 2
之后,我通过将输入文件中每个单词后的 space 替换为逗号来测试代码。
现在我已将输入还原为与以前相同,但现在输出中的 WordCount 是预期结果的两倍。
Bye 2
Hello 2
World 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 中每个词的值是如何分组的,因为它对特定的每个值求和 word.Where 它检查同一个词是否有两个计数。
谢谢
必须将输入文件夹作为输入路径,其中必须有两个内容相同的文件,这可能是重复计数的原因