使用Hadoop Streaming时通过脚本运行Java应用程序:java.lang.NoClassDefFoundError

时间:2012-09-02 10:22:22

标签: java shell hadoop streaming

我使用Hadoop Streaming在tcsh中执行脚本文件:

-- First copy the jar files to the Hadoop filesystem,
   so that they are next to inputdir and outputdir. --
cp App/* /hadoop/jardir/

-- Run Hadoop --
hadoop jar /usr/lib/hadoop/contrib/streaming/hadoop-streaming-0.20.2-cdh3u4.jar \\
-D mapred.task.timeout=120000000 \\
-input "/hadoop/inputdir/" -output "/hadoop/outputdir/" \\
-mapper script.sh -reducer script.sh -file script.sh \\
-jobconf mapred.map.tasks=1 -jobconf mapred.reduce.tasks=0 >>& log.txt

此脚本文件调用Java,如下所示:

java -cp /hadoop/jardir/SomeJavaApp.jar:/hadoop/jardir/* some.JavaApplication

即使我明确告诉Java类路径,运行它也会失败:

Exception in thread "main" java.lang.NoClassDefFoundError: some/JavaApplication
Caused by: java.lang.ClassNotFoundException: some.JavaApplication
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: some.JavaApplication.  Program will exit.

我已经尝试了各种建议,例如设置Hadoops -libjars参数,但这没有帮助。

如何在使用Hadoop Streaming时通过脚本运行Java应用程序?

1 个答案:

答案 0 :(得分:2)

我认为问题是你的jar没有被复制到分布式缓存中。 尝试通过-files选项将jar与shell脚本一起发送。例如:

hadoop jar /home/user/hadoop-distrib/lib/hadoop-streaming.jar \
    -files runjava.sh,hadooptest.jar  \
    -D mapreduce.job.reduces=0 \
    -D mapred.job.name=TestJob \
    -input /user/hadoop/test \
    -output /user/hadoop/test/res \
    -mapper 'runjava.sh'

我假设您正在从存储hadooptest.jarrunjava.sh的目录中运行您的流媒体作业。

runjava.sh:

java -cp /home/user/test/hadooptest.jar:/home/user/hadoop-distrib/lib/* com.example.hadooptest.StreamTest


StreamTest.java:

...
public class StreamTest {

    public static void main(String[] args) throws Exception {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s = null;
        while ((s = in.readLine()) != null && s.length() != 0) {
            System.out.println(s);
        }
    }
}

此示例在版本0.20-append-r1056497上正常工作。