mapreduce不还原?

kt06eoxx  于 2021-05-30  发布在  Hadoop
关注(0)|答案(1)|浏览(312)

我在听教程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

我一定错过了一些非常琐碎的事情,但我不知道是什么。救命啊。。

bnlyeluc

bnlyeluc1#

这个问题的根本原因是,您没有调用 reduce() 用精确的 Signature 按要求呼叫 Hadoop . 签名如下(此处参考)

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

自从你的 reduce() 不符合 Signature , Hadoop 将调用默认的identityreducer,它输出相同的输入。
因此,只有您得到的map输出与reduce输出相同。
对于这个问题,我可以给你两个建议,
首先:尝试下面的代码

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);
    }
  }

第二:第二个解决方案很简单,
不用手动定义reduce类,只需将reducer类设置为 IntSumReducer 或者 LongSumReducer 它将执行与上述代码相同的操作。
所以不要定义 WordCountReducer 类并添加以下代码,

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

基于你想要的计数类型。
希望有帮助!

相关问题