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



我在Hadoop Map/Reduce作业中遇到了一个奇怪的问题。作业正确提交,运行,但产生不正确/奇怪的结果。似乎映射器和减速器根本没有运行。输入文件转换自:

12
16
132
654
132
12

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

我假设第一列是映射器之前生成的对键,但映射器和化简器似乎都没有运行。当我使用旧 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.");
    }
}

也许我错过了一些明显的东西,但谁能阐明这里可能出现的问题?

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

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

这应该是——

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

您使用 Map 和 Reduce 扩展了映射器和化简器类,但没有在作业中使用它们。

相关内容

  • 没有找到相关文章

最新更新