简单的单词计数MapReduce示例产生奇怪的结果

时间:2013-10-04 14:42:02

标签: java hadoop mapreduce hortonworks-data-platform

我遇到了Hadoop Map / Reduce工作的奇怪问题。作业正确提交,运行,但产生错误/奇怪的结果。似乎映射器和减速器根本不运行。输入文件从以下转换:

12
16
132
654
132
12

0   12
4   16
8   132
13  654
18  132
23  12

我假设第一列是映射器之前对的生成键,但mapper和reducer似乎都没有运行。当我使用旧的API时,工作运行正常。

下面提供了该工作的来源。我使用Hortonworks作为平台。

public class HadoopAnalyzer
{
    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
    {
        JobConf conf = new JobConf(HadoopAnalyzer.class);
        conf.setJobName("wordcount");
        conf.set("mapred.job.tracker", "192.168.229.128:50300");
        conf.set("fs.default.name", "hdfs://192.168.229.128:8020");
        conf.set("fs.defaultFS", "hdfs://192.168.229.128:8020");
        conf.set("hbase.master", "192.168.229.128:60000");
        conf.set("hbase.zookeeper.quorum", "192.168.229.128");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        System.out.println("Executing job.");
        Job job = new Job(conf, "job");
        job.setInputFormatClass(InputFormat.class);
        job.setOutputFormatClass(OutputFormat.class);
        job.setJarByClass(HadoopAnalyzer.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        TextInputFormat.addInputPath(job, new Path("/user/usr/in"));
        TextOutputFormat.setOutputPath(job, new Path("/user/usr/out"));
        job.setMapperClass(Mapper.class);
        job.setReducerClass(Reducer.class);
        job.waitForCompletion(true);
        System.out.println("Done.");
    }
}

也许我错过了一些显而易见的事情,但是有人能说清楚这里可能出现的问题吗?

1 个答案:

答案 0 :(得分:3)

输出符合预期,因为您使用了以下内容,

job.setMapperClass(Mapper.class);
job.setReducerClass(Reducer.class);

应该是 -

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

您使用Map和Reduce扩展了Mapper和Reducer类,但没有在您的工作中使用它们。