2012-08-14 26 views
3

解決方案:使用一個更好的教程 - http://hadoop.apache.org/mapreduce/docs/r0.22.0/mapred_tutorial.htmlHadoop MapReduce的新手,在mapred.Reducer上獲取NoSuchMethodException。 <init>()

我剛開始的MapReduce工作,和我遇到一個奇怪的錯誤,我一直無法通過谷歌來回答。我正在做一個基本的字計數程序,但是當我運行它,我在收到以下錯誤減少:

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) 

的字計數程序是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); 

} 
} 

回答

8

問題不在於您選擇的API。穩定版本(mapred。*)和不斷髮展的(mapreduce。*)API都得到完全支持,並且框架本身攜帶兩種測試,以確保跨版本無迴歸/破壞。

的問題是這一行:

conf.setReducerClass(Reducer.class); 

你設置減速接口有作還原劑,當您應該設置你的減速接口的實現來代替。將其更改爲:

conf.setReducerClass(Reduce.class); 

將解決此問題。

+0

猜測我可以感謝Eclipse的那個自動完成功能。 – HypnoticSheep 2012-09-10 17:12:37

1

檢查,以確保您使用的hadoop.mapreduce包,而不是hadoop.mapred包。與當前版本的mapreduce類相比,mapred包更老,並且對類有不同的方法。

+0

我目前使用hadoop.mapred包,因爲這是本教程使用的。切換到hadoop.mapreduce會在MapReduceBase,Mapper,Reducer等等上導致錯誤。交換機是否必要,或將hadoop.mapred仍然工作? – HypnoticSheep 2012-08-14 22:28:30

+0

包裝的使用需要在單一作業中保持一致。如果一切都匹配,我會撤回答案。 – 2012-08-14 22:36:37

+0

你說得對,實際上,mapred軟件包不能再工作了。設法找到一個使用mapreduce軟件包的較新教程,但不確定它爲什麼不是Apache網站上的默認教程。無論如何,它的工作原理。謝謝! – HypnoticSheep 2012-08-14 23:27:24