3

解决方案:使用更好的教程 - http://hadoop.apache.org/mapreduce/docs/r0.22.0/mapred_tutorial.html

我刚开始使用 MapReduce,遇到了一个奇怪的错误,我无法通过 Google 回答。我正在制作一个基本的 WordCount 程序,但是当我运行它时,在 Reduce 期间出现以下错误:

java.lang.RuntimeException: java.lang.NoSuchMethodException: org.apache.hadoop.mapred.Reducer.<init>()
at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:115)
at org.apache.hadoop.mapred.ReduceTask.runOldReducer(ReduceTask.java:485)
at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:420)
at org.apache.hadoop.mapred.Child$4.run(Child.java:255)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1121)
at org.apache.hadoop.mapred.Child.main(Child.java:249)

WordCount 程序是 Apache MapReduce 教程中的一个。我在 Mountain Lion 上以伪分布式模式运行 Hadoop 1.0.3,我认为所有这些都运行良好,因为示例都正常执行。有任何想法吗?

编辑:这是我的参考代码:

package mrt;

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.mapred.*;
import org.apache.hadoop.util.*;

public class WordCount {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {

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

    public void map(LongWritable key, Text value, OutputCollector<Text,IntWritable> output, Reporter reporter)
      throws IOException{

        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);
        while(tokenizer.hasMoreTokens()){
            word.set(tokenizer.nextToken());
            output.collect(word,one);
        }
    }
}

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

    public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter)
      throws IOException{

        int sum = 0;

        while(values.hasNext()){
            sum += values.next().get();
        }

        output.collect(key, new IntWritable(sum));

    }
}

public static void main(String[] args) throws Exception{

    JobConf conf = new JobConf(WordCount.class);
    conf.setJobName("Wordcount");

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

    conf.setMapperClass(Map.class);
    conf.setCombinerClass(Reduce.class);
    conf.setReducerClass(Reducer.class);

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

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

    JobClient.runJob(conf);

}
}
4

2 回答 2

9

问题不在于您选择的 API。完全支持稳定的 (mapred.*) 和不断发展的 (mapreduce.*) API,并且框架本身对这两种 API 进行了测试,以确保跨版本不会出现回归/破坏。

问题是这一行:

conf.setReducerClass(Reducer.class);

当您应该设置 Reducer 接口的实现时,您将在那里设置 Reducer 接口作为 Reducer。将其更改为:

conf.setReducerClass(Reduce.class);

会修复它。

于 2012-09-10T11:34:09.720 回答
1

检查以确保您使用的是 hadoop.mapreduce 包而不是 hadoop.mapred 包。与当前版本的 mapreduce 类相比,mapred 包较旧,并且在类上具有不同的方法。

于 2012-08-14T22:20:47.187 回答