Map Reduce字数统计程序给出“class not found exception”

时间:2017-10-08 09:02:20

标签: hadoop mapreduce hdfs word-count

我的hadoop版本是: 2.8.1

我正在尝试运行Apache Hadoop 2.8.0

中的mapreduce示例

WordCount源代码如下。(与Apache Hadoop 2.8.0示例中给出的相同)

import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount {

public static class TokenizerMapper
   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);
   }
  }
}

public static class IntSumReducer
   extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();

public void reduce(Text key, Iterable<IntWritable> values,
                   Context context
                   ) throws IOException, InterruptedException {
  int sum = 0;
  for (IntWritable val : values) {
    sum += val.get();
  }
  result.set(sum);
  context.write(key, result);
 }
}

 public static void main(String[] args) throws Exception {
  Configuration conf = new Configuration();
  Job job = Job.getInstance(conf, "word count");
  job.setJarByClass(WordCount.class);
  job.setMapperClass(TokenizerMapper.class);
  job.setCombinerClass(IntSumReducer.class);
  job.setReducerClass(IntSumReducer.class);
  job.setOutputKeyClass(Text.class);
  job.setOutputValueClass(IntWritable.class);
  FileInputFormat.addInputPath(job, new Path(args[0]));
  FileOutputFormat.setOutputPath(job, new Path(args[1]));
  System.exit(job.waitForCompletion(true) ? 0 : 1);
 }
}

我通过插入上面的代码创建了一个WordCount.java文件。 然后我编译了它。

javac -cp $HADOOP_CLASSPATH /sharedFiles/WordCount.java

然后我结合使用WordCount * .classes来创建一个wc.jar文件。

jar cf /sharedFiles/wc.jar /sharedFiles/WordCount*.class

在sharedFiles文件夹中,文件如下所示。

ls /sharedFiles
history  wc.jar  WordCount.class  
WordCount$IntSumReducer.class        
WordCount.java    WordCount$TokenizerMapper.class

然后我尝试运行mapreduce命令。

hadoop jar /sharedFiles/wc.jar wordcount /sharedFiles/history /sharedFiles /output

它引发了我这个错误。

Exception in thread "main" java.lang.ClassNotFoundException: wordcount

我注意到在相应的文件夹中没有创建驱动程序类“wordcount”。我可以做些什么来创建这个课程?在本教程中,他们没有提到创建该文件的任何其他步骤。

谢谢。

1 个答案:

答案 0 :(得分:0)

@Yash,请你验证你的命令。我认为你用小写字母写了类名,同时运行Hadoop jar命令。

hadoop jar /sharedFiles/wc.jar WordCount /sharedFiles/history  /sharedFiles /output

我希望这会对你有所帮助。