MapReduce没有减少?

时间:2015-04-08 01:46:23

标签: hadoop mapreduce

我正在关注http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html上的教程,这是我的代码

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Iterator;

public class WordCount {
    public static class WordCountMapper extends Mapper<Object, Text, Text, IntWritable> {

        private Text word = new Text();
        private final IntWritable one = new IntWritable(1);

        @Override
        public void map(Object key, Text val, Context context) throws IOException, InterruptedException {
            String line = val.toString();
            StringTokenizer tokenizer = new StringTokenizer(line.toLowerCase());
            while (tokenizer.hasMoreTokens()) {
                word.set(tokenizer.nextToken());
                context.write(word, one);
            }
        }
    }

    public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

        public void reduce(Text key, Iterator<IntWritable> value, Context context) throws IOException, InterruptedException {
            int sum = 0;
            while (value.hasNext()) {
                IntWritable val = (IntWritable) value.next();
                sum += val.get();
            }
        context.write(key, new IntWritable(sum));
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration config = new Configuration();
        Job job = Job.getInstance(config, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setCombinerClass(WordCountReducer.class);
        job.setReducerClass(WordCountReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new     Path("/user/Icarus/words.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/user/Icarus/words.out"));
        job.waitForCompletion(true);
    }
}

但是当我运行而不是计算单词频率时,我得到了这个:

bye 1
goodbye 1
hadoop  1
hadoop  1
hello   1
hello   1
hello   1
world   1

我必须错过一些非常微不足道的事情,但我无法弄清楚是什么。请帮助..

1 个答案:

答案 0 :(得分:0)

此问题的根本原因是,您没有使用reduce()调用所需的确切Signature来调用Hadoop。签名应如下(参考here

protected void reduce(KEYIN key, Iterable<VALUEIN> values, org.apache.hadoop.mapreduce.Reducer.Context context)
               throws IOException, InterruptedException

由于reduce()Signature不匹配,Hadoop会调用默认IdentityReducer,输出相同的输入。
因此,只有您获得与减少输出相同的Map输出。

对于这个问题,我可以建议你2个解决方案,
首先:尝试以下代码

public static class WordCountReducer
       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);
    }
  }  

第二:第二种解决方案很简单,
不要手动定义减少类,只需将Reducer类设置为IntSumReducerLongSumReducer,这将与上面的代码相同。
所以不要定义WordCountReducer类并添加以下代码,

job.setReducerClass(LongSumReducer.class); or  
job.setReducerClass(IntSumReducer.class);

根据您想要的计数类型。

希望它有所帮助!