长时间没学习,重新调试mapreduce程序WordCount的时候,以前写好并调试通过的程序出问题了,报错如下:
Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class hadoopProj.wordcount.WordCount$TokenizerMapper not found
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2195)
at org.apache.hadoop.mapreduce.task.JobContextImpl.getMapperClass(JobContextImpl.java:186)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:745)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:163)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
死活不明白,以前明明调试没问题的,这次并没有重新编译,还是用的以前编好的jar包,也重启hadoop,还是一样的,于是详查报错的说明,在开头处看到了这样一段:
start main…
start job wait…
17/01/10 15:05:33 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032
17/01/10 15:05:33 WARN mapreduce.JobResourceUploader: No job jar file set. User classes may not be found. See Job or Job#setJar(String)
“No job jar file set.”是什么意思?要没找到jar文件还能执行吗,试着在命令行中改了个不存在的jar文件名替代,报出的错是不一样的,说明jar文件还是找到了,既然找到了为什么又这么提示?
上网搜有说Eclipse版本问题的,有说没有添加依赖的,有说没有上传到hdfs的,好像都不沾边,我用的是控制台,以前运行也不需要添加什么上传什么,于是想是不是跟jar文件路径的摆放有关系(因为之前没少在这方面出错),幸好我以前调试偷懒写了个shell用来执行编译和调试,可以从里头看出来之前的路径结构以及所有的指令,对比之后发现跟我现在不同的就一处:我之前的WordCount.jar文件放在/hadoopProj下,但是现在是放在/hadoopProj/wordcount下。于是我将jar文件拷贝到/hadoopProj下再试(当然对应的指令中的路径也要改),通过了!然后我就想是不是跟环境变量什么的有关系,我检查了没有添加/hadoopProj,然后又试着拷贝WordCount.jar到任意路径都可以运行通过,唯独/hadoopProj/wordcount。于是我又开始审视代码,这里也贴一下吧
package hadoopProj.wordcount;
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.jar文件放在/hadoopProj/wordcount下的时候,上述代码中橙色高亮的一句
job.setJarByClass(WordCount.class);
没有成功(至于为什么程序都已经从该jar文件执行了还要通过这个方法来查找这个jar文件,我想应该是有时候需要查找的类所在位置并非在当前jar文件中,也就是说当前任务的实现未必包含在当前jar中,比如这个例子,main函数如果打包在另一个jar文件中呢?)。那是为什么没有成功呢,应该是因为/hadoopProj/wordcount的目录结构如下
hadoopProj
-wordcount
-WordCount.jar
-WordCount.java
-WordCount.class
-…
在job.setJarByClass(WordCount.class)执行的时候,程序从当前任务的main函数所在的文件路径开始查找WordCount.class,刚好就查到了hadoopProj/wordcount,这是一个文件夹,而不是一个jar文件,所以失败了,提示“No job jar file set.”,而如果jar文件不跟WordClass.class文件放在同一目录下,自然程序查找当前目录找不到就会去jar文件里找,就能返回该jar文件。为了验证这个想法,我把/hadoopProj/wordcount下的WordCount.class文件删除掉,果然没问题,再添加回去,又是同样出错。
所以结论是:我不应该把WordCount.jar文件放在/hadoopProj/wordcount下。本来以前jar文件打好包的时候是在hadoopProj同级目录下的,做完实验后为了归类我把它放在了wordcount下,才有了这次莫名其妙的错误。