我正在研究hadoop 1.2.1的字数示例。但是必须有所改变,因为我似乎无法使其正常工作。

这是我的Reduce类(class):

public static class Reduce extends Reducer<WritableComparable, Writable, WritableComparable, Writable> {

    public void reduce(WritableComparable key,
                       Iterator<Writable> values,
                       OutputCollector<WritableComparable, NullWritable> output,
                       Reporter reporter) throws IOException {

        output.collect(key, NullWritable.get());

    }

}

而我的主要功能是:
public static void main(String[] args) throws Exception {

    JobConf jobConf = new JobConf(MapDemo.class);

    jobConf.setNumMapTasks(10);
    jobConf.setNumReduceTasks(1);

    jobConf.setJobName("MapDemo");

    jobConf.setOutputKeyClass(Text.class);
    jobConf.setOutputValueClass(NullWritable.class);

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

    jobConf.setInputFormat(TextInputFormat.class);
    jobConf.setOutputFormat(TextOutputFormat.class);

    FileInputFormat.setInputPaths(jobConf, new Path(args[0]));
    FileOutputFormat.setOutputPath(jobConf, new Path(args[1]));

    JobClient.runJob(jobConf);
}

我的IDE告诉我有一个错误,由Maven证实:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] com/example/mapreduce/MapDemo.java:[71,16] method setReducerClass in class org.apache.hadoop.mapred.JobConf cannot be applied to given types;
required: java.lang.Class<? extends org.apache.hadoop.mapred.Reducer>
found: java.lang.Class<com.example.mapreduce.MapDemo.Reduce>
reason: actual argument java.lang.Class<com.example.mapreduce.MapDemo.Reduce> cannot be converted to java.lang.Class<? extends org.apache.hadoop.mapred.Reducer> by method invocation conversion
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.679s
[INFO] Finished at: Mon Sep 16 09:23:08 PDT 2013
[INFO] Final Memory: 17M/202M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile (default-compile) on project inventory: Compilation failure
[ERROR] com/example/mapreduce/MapDemo.java:[71,16] method setReducerClass in class org.apache.hadoop.mapred.JobConf cannot be applied to given types;
[ERROR] required: java.lang.Class<? extends org.apache.hadoop.mapred.Reducer>
[ERROR] found: java.lang.Class<com.example.mapreduce.MapDemo.Reduce>

我相信网上的字数示例已过时1.2.1。我该如何解决?是否有人链接到有效的1.2.1字数统计Java源代码?

最佳答案

您跟随哪个链接?我从未见过这种WC。但是,无论您遵循什么,都肯定已经过时了,因为它正在使用旧的API。我怀疑您是否遵循正确。

这应该工作:

public class WordCount {
    /**
     * The map class of WordCount.
     */
    public static class TokenCounterMapper extends
            Mapper<Object, Text, Text, IntWritable> {

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

        public void map(Object key, Text value, Context context)
                throws IOException, InterruptedException {

            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
        }
    }

    /**
     * The reducer class of WordCount
     */
    public static class TokenCounterReducer extends
            Reducer<Text, IntWritable, Text, IntWritable> {
        public void reduce(Text key, Iterable<IntWritable> values,
                Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }
            context.write(key, new IntWritable(sum));
        }
    }

    /**
     * The main entry point.
     */
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/core-site.xml"));
        conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/hdfs-site.xml"));
        conf.set("fs.default.name", "hdfs://localhost:9000");
        conf.set("mapred.job.tracker", "localhost:9001");
        Job job = new Job(conf, "WordCount");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenCounterMapper.class);
        job.setReducerClass(TokenCounterReducer.class);
        job.setNumReduceTasks(2);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path("/inputs/demo.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/outputs/1111223"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

很少有观察可做:
  • 您没有发出任何计数,因为我可以看到 reducer 发出 NullWritable 。它将仅发出密钥而没有任何计数。
  • 输入和输出键/值时,请使用正确类型
  • 使用新的 API 。它更干净,更好。
  • 09-04 05:34