【问题标题】:Mapreduce WordCount Example giving wrong outputMapreduce WordCount 示例给出错误的输出
【发布时间】: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


【解决方案1】:

必须将输入文件夹作为输入路径,其中必须有两个内容相同的文件,这可能是重复计数的原因

【讨论】:

  • 这是真的,我将输入文件夹作为路径,但只有一个文件。
  • 是的,您是正确的。其他文件未通过 UI 显示。当我通过终端检查时,它的名称为“file01~”。我删除了它,现在它正在工作。你能告诉我为什么这个文件是在那里创建的吗?你也可以帮我解决我在帖子中的其他问题。
  • 我已经尝试了你的代码,它给了我正确的结果,只需尝试提供文件路径而不是文件夹路径作为输入
  • 值按 key 分组在 reducer 中,在此程序中,您将 word 配置为 mapper 中的键,因此 reducer 获得了关键记录并为每个键调用 reduce 方法,因此在您的情况下,reduce 方法调用三次对于 Hello,Bye 和 World,所以对于 Hello,您的键和值数组就像 (Hello,[1]) 对于 Bye 它就像 (Bye,[1]) 而对于 World 它就像 (World,[1,1] )
猜你喜欢
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多