我的Hadoop版本是:2.8.1的Map Reduce字數程序給出「類未發現異常」
我試圖運行映射縮減例子是Apache Hadoop 2.8.0
字計數的源代碼是下方。(同在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的完整路徑。 但它爲我做了詭計。謝謝! – Yash