使用hadoop运行自己写的java类报错:
/home/hadoop/bin/hadoop MaxTemperature /home/hadoop/input/sample.txt outputException in thread "main" java.lang.NoClassDefFoundError: MaxTemperatureCaused by: java.lang.ClassNotFoundException: MaxTemperature 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: MaxTemperature. Program will exit.查了很多资料,总结运行java类的步骤。
步骤1:设置java环境变量,编辑 conf/hadoop-env.sh文件,至少需要将JAVA_HOME设置为Java安装根路径。
步骤2:行java程序时要先编译成class文件,才能用hadoop命令,但是编译的时候会有如下错误:
axTemperature.java:5: 软件包 org.apache.hadoop.fs 不存在import org.apache.hadoop.fs.Path; ^MaxTemperature.java:6: 软件包 org.apache.hadoop.io 不存在import org.apache.hadoop.io.IntWritable; ^MaxTemperature.java:7: 软件包 org.apache.hadoop.io 不存在import org.apache.hadoop.io.Text; ^MaxTemperature.java:8: 软件包 org.apache.hadoop.mapred 不存在import org.apache.hadoop.mapred.FileInputFormat; ^MaxTemperature.java:9: 软件包 org.apache.hadoop.mapred 不存在import org.apache.hadoop.mapred.FileOutputFormat;....上面的信息很明确,找不到hadoop相关的java类,请在CLASSPATH设置一下
export CLASSPATH=.:/home/hadoop/hadoop-0.20.2-core.jar:$CLASSPATH步骤3:运行自己写的java类。
有的时候报错:
root@tianbaoxing-virtual-machine:/home/hadoop/java# /home/hadoop/bin/hadoop MaxTemperature /home/hadoop/input/sample.txt outputException in thread "main" java.lang.NoClassDefFoundError: MaxTemperatureCaused by: java.lang.ClassNotFoundException: MaxTemperature 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: MaxTemperature. Program will exit.上面的信息报错找不到运行的类, 是因为没有设置classpath,在hadoop环境下怎么设置classpath呢?
export HADOOP_CLASSPATH=/home/hadoop/java我把要运行的java类全部放在了/home/hadoop/java 这个文件夹下面了。
最后正确的运行结果:
/home/hadoop/bin/hadoop MaxTemperature /home/hadoop/input/sample.txt output13/04/10 11:00:31 INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=13/04/10 11:00:31 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.13/04/10 11:00:32 WARN mapred.JobClient: No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).13/04/10 11:00:32 INFO mapred.FileInputFormat: Total input paths to process : 113/04/10 11:00:33 INFO mapred.JobClient: Running job: job_local_000113/04/10 11:00:33 INFO mapred.FileInputFormat: Total input paths to process : 113/04/10 11:00:33 INFO mapred.MapTask: numReduceTasks: 113/04/10 11:00:33 INFO mapred.MapTask: io.sort.mb = 10013/04/10 11:02:48 INFO mapred.MapTask: data buffer = 79691776/9961472013/04/10 11:02:48 INFO mapred.MapTask: record buffer = 262144/32768013/04/10 11:02:48 INFO mapred.LocalJobRunner: file:/home/hadoop/input/sample.txt:0+233613/04/10 11:02:48 INFO mapred.JobClient: map 0% reduce 0%13/04/10 11:02:48 INFO mapred.MapTask: Starting flush of map output13/04/10 11:02:52 INFO mapred.MapTask: Finished spill 013/04/10 11:02:52 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting13/04/10 11:02:52 INFO mapred.LocalJobRunner: file:/home/hadoop/input/sample.txt:0+233613/04/10 11:02:52 INFO mapred.TaskRunner: Task 'attempt_local_0001_m_000000_0' done.13/04/10 11:02:52 INFO mapred.LocalJobRunner: 13/04/10 11:02:52 INFO mapred.Merger: Merging 1 sorted segments13/04/10 11:02:52 INFO mapred.JobClient: map 100% reduce 0%13/04/10 11:02:52 INFO mapred.Merger: Down to the last merge-pass, with 1 segments left of total size: 189 bytes13/04/10 11:02:52 INFO mapred.LocalJobRunner: 13/04/10 11:02:53 INFO mapred.TaskRunner: Task:attempt_local_0001_r_000000_0 is done. And is in the process of commiting13/04/10 11:02:53 INFO mapred.LocalJobRunner: 13/04/10 11:02:53 INFO mapred.TaskRunner: Task attempt_local_0001_r_000000_0 is allowed to commit now13/04/10 11:02:53 INFO mapred.FileOutputCommitter: Saved output of task 'attempt_local_0001_r_000000_0' to file:/home/hadoop/java/output13/04/10 11:02:53 INFO mapred.LocalJobRunner: reduce > reduce13/04/10 11:02:53 INFO mapred.TaskRunner: Task 'attempt_local_0001_r_000000_0' done.13/04/10 11:02:53 INFO mapred.JobClient: map 100% reduce 100%13/04/10 11:02:53 INFO mapred.JobClient: Job complete: job_local_000113/04/10 11:02:53 INFO mapred.JobClient: Counters: 1313/04/10 11:02:53 INFO mapred.JobClient: FileSystemCounters13/04/10 11:02:53 INFO mapred.JobClient: FILE_BYTES_READ=3048513/04/10 11:02:53 INFO mapred.JobClient: FILE_BYTES_WRITTEN=5240013/04/10 11:02:53 INFO mapred.JobClient: Map-Reduce Framework13/04/10 11:02:53 INFO mapred.JobClient: Reduce input groups=213/04/10 11:02:53 INFO mapred.JobClient: Combine output records=013/04/10 11:02:53 INFO mapred.JobClient: Map input records=1713/04/10 11:02:53 INFO mapred.JobClient: Reduce shuffle bytes=013/04/10 11:02:53 INFO mapred.JobClient: Reduce output records=213/04/10 11:02:53 INFO mapred.JobClient: Spilled Records=3413/04/10 11:02:53 INFO mapred.JobClient: Map output bytes=15313/04/10 11:02:53 INFO mapred.JobClient: Map input bytes=233613/04/10 11:02:53 INFO mapred.JobClient: Combine input records=013/04/10 11:02:53 INFO mapred.JobClient: Map output records=1713/04/10 11:02:53 INFO mapred.JobClient: Reduce input record=17