我使用 Apache Hadoop 1.2.1 开发了一个 map-reduce 程序。我使用 Eclipse IDE 进行了初始开发,以模拟 hadoop 分布式计算环境,所有输入和输出文件都来自我的本地文件系统。该程序将在 Eclipse 中毫无问题地执行。然后,我使用 Eclipse 创建了一个 JAR 文件,并尝试在我的一个集群的 hadoop 机器上运行它并收到错误:
这是我设置和运行 hadoop 作业的代码:
String outputPath = "/output";
String hadoopInstructionsPath = args[0];
Job job = new Job();
job.setJarByClass(Main.class); //setJarByClass is here but not found apparently?!?
job.setJobName("KLSH");
FileInputFormat.addInputPath(job, new Path(hadoopInstructionsPath));
FileOutputFormat.setOutputPath(job,new Path(outputPath));
job.setMapperClass(KLSHMapper.class);
job.setReducerClass(KLSHReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
System.exit(job.waitForCompletion(true) ? 0:1);
boolean success = job.waitForCompletion(true);
return success ? 0 : 1;
然后,我使用 eclipse 创建一个 jar,使用 File -> Export -> Runnable JAR file 创建要在集群上运行的 JAR 文件。
我用来运行作业的命令如下(KLSH.jar 是 JAR 文件的名称,/hadoopInstruction 是 args[0] 输入参数,imageFeature.Main/ 指定主类在哪里)
./hadoop jar ./KLSH.jar /hadoopInstructions imageFeatures.Main/
这会产生以下输出:
14/11/12 11:11:48 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
14/11/12 11:11:48 WARN mapred.JobClient: No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
14/11/12 11:11:48 INFO input.FileInputFormat: Total input paths to process : 1
14/11/12 11:11:48 INFO util.NativeCodeLoader: Loaded the native-hadoop library
14/11/12 11:11:48 WARN snappy.LoadSnappy: Snappy native library not loaded
14/11/12 11:11:49 INFO mapred.JobClient: Running job: job_201411051030_0022
14/11/12 11:11:50 INFO mapred.JobClient: map 0% reduce 0%
14/11/12 11:11:56 INFO mapred.JobClient: Task Id : attempt_201411051030_0022_m_000000_0, Status : FAILED
java.lang.RuntimeException: java.lang.ClassNotFoundException: imageFeatures.KLSHMapper
...
所以它出错了,因为它找不到映射器类。有“No job jar file set”的警告,但我感觉我在第一块代码中指定了job.setJarByClass,所以不知道为什么会抛出这个错误......
我也知道 KLSHMapper 类在 JAR 中,因为如果我运行以下命令:
jar tf KLSH.jar
我得到了很多输出,但这里是输出的一部分:
...
imageFeatures/Main.class
imageFeatures/Feature.class
imageFeatures/FileLoader.class
imageFeatures/KLSHMapper.class
...
很明显,KLSHMapper 类在那里......我尝试修改我的 hadoop 类路径以包含 KLSH.jar 路径,我尝试将 KLSH.jar 复制到 DFS 并尝试使用该路径而不是路径我的本地文件系统,我还尝试使用 -libjars 说明符执行该作业。无论我尝试什么,hadoop 似乎都无法找到我的 Mapper 类。有人可以指出我做错了什么吗?我似乎无法从我在 Eclipse 中工作的代码跳转到使其在实际的 Hadoop 集群上工作。谢谢!