hadoop:reducer类即使使用重写也不调用

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

我在hadoop中尝试了mapreduce wordcount代码,但是没有调用reducer类,程序在运行mapper类之后终止。

import java.io.IOException;
import java.util.*;

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

public class WordCount {

 public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
    @Override 
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()) {
            word.set(tokenizer.nextToken());
            context.write(word, one);
        }
    }
 } 

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

     @Override
    public void reduce(Text key, Iterable<IntWritable> values, Context context) 
      throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable val : values) {
            sum += val.get();
        }
        context.write(key, new IntWritable(sum));
    }
 }

 public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();

        Job job = new Job(conf, "wordcount");

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    job.setMapperClass(Map.class);
    job.setReducerClass(Reduce.class);

    job.setInputFormatClass(TextInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));

    job.waitForCompletion(true);
 }

}

我甚至按要求重写了这些类。
ide:月 eclipse 月神
hadoop:2.5版

bgtovc5b

bgtovc5b1#

作业对象形成作业的规范,并使您可以控制作业的运行方式。当我们在hadoop集群上运行这个作业时,我们将把代码打包成jar文件(hadoop将在集群中分发该文件)。
我们可以在作业的setjarbyclass()方法中传递一个类,而不是显式地指定jar文件的名称,hadoop将使用这个类通过查找包含这个类的jar文件来定位相关的jar文件。
我看不到main方法中的语句。因此,请包含这些内容,然后编译并运行代码。
job.setjarbyclass(wordcount.class);

相关问题