【问题标题】:Map Reduce word count program gives "class not found exception"Map Reduce 字数程序给出“类未找到异常”
【发布时间】:2017-10-08 09:02:20
【问题描述】:

我的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”没有在相应的文件夹中创建。我能做些什么来创建那个类吗?在本教程中,他们没有提到创建该文件的任何额外步骤。

谢谢。

【问题讨论】:

  • 你在创建JAR的时候,是否认为sharedFiles文件夹是一个包?你试过sharedFiles.WordCount吗?
  • @cricket_007 是的,它奏效了。除此之外,还需要将包名称添加到 WordCount,java 文件中。我不明白为什么需要它,因为在命令行中,我们指定了 WordCount.java 的完整路径。然而,它对我有用。谢谢!

标签: hadoop mapreduce hdfs word-count


【解决方案1】:

@Yash,您能否验证您的命令。我想你在运行 Hadoop jar 命令时用小写字母写了类名。

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

希望对你有帮助。

【讨论】:

  • 我更改了它,但仍然收到错误。 hadoop jar /sharedFiles/wc.jar WordCount /sharedFiles/history /sharedFiles/output 线程“main”中的异常 java.lang.ClassNotFoundException: WordCount
  • 您能否检查一下 tar -tf WC.jar 命令。它将显示所需的类是否正确生成。
猜你喜欢
  • 2019-09-20
  • 2012-09-21
  • 2019-07-18
  • 2014-01-04
  • 2022-01-21
  • 2013-11-13
  • 2023-02-23
  • 2020-11-29
  • 1970-01-01
相关资源
最近更新 更多