hadoop运行java类

使用hadoop运行自己写的java类报错:

/home/hadoop/bin/hadoop MaxTemperature /home/hadoop/input/sample.txt output
Exception in thread "main" java.lang.NoClassDefFoundError: MaxTemperature
Caused 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 output
Exception in thread "main" java.lang.NoClassDefFoundError: MaxTemperature
Caused 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 output
13/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 : 1
13/04/10 11:00:33 INFO mapred.JobClient: Running job: job_local_0001
13/04/10 11:00:33 INFO mapred.FileInputFormat: Total input paths to process : 1
13/04/10 11:00:33 INFO mapred.MapTask: numReduceTasks: 1
13/04/10 11:00:33 INFO mapred.MapTask: io.sort.mb = 100
13/04/10 11:02:48 INFO mapred.MapTask: data buffer = 79691776/99614720
13/04/10 11:02:48 INFO mapred.MapTask: record buffer = 262144/327680
13/04/10 11:02:48 INFO mapred.LocalJobRunner: file:/home/hadoop/input/sample.txt:0+2336
13/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 output
13/04/10 11:02:52 INFO mapred.MapTask: Finished spill 0
13/04/10 11:02:52 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting
13/04/10 11:02:52 INFO mapred.LocalJobRunner: file:/home/hadoop/input/sample.txt:0+2336
13/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 segments
13/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 bytes
13/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 commiting
13/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 now
13/04/10 11:02:53 INFO mapred.FileOutputCommitter: Saved output of task 'attempt_local_0001_r_000000_0' to file:/home/hadoop/java/output
13/04/10 11:02:53 INFO mapred.LocalJobRunner: reduce > reduce
13/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_0001
13/04/10 11:02:53 INFO mapred.JobClient: Counters: 13
13/04/10 11:02:53 INFO mapred.JobClient:   FileSystemCounters
13/04/10 11:02:53 INFO mapred.JobClient:     FILE_BYTES_READ=30485
13/04/10 11:02:53 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=52400
13/04/10 11:02:53 INFO mapred.JobClient:   Map-Reduce Framework
13/04/10 11:02:53 INFO mapred.JobClient:     Reduce input groups=2
13/04/10 11:02:53 INFO mapred.JobClient:     Combine output records=0
13/04/10 11:02:53 INFO mapred.JobClient:     Map input records=17
13/04/10 11:02:53 INFO mapred.JobClient:     Reduce shuffle bytes=0
13/04/10 11:02:53 INFO mapred.JobClient:     Reduce output records=2
13/04/10 11:02:53 INFO mapred.JobClient:     Spilled Records=34
13/04/10 11:02:53 INFO mapred.JobClient:     Map output bytes=153
13/04/10 11:02:53 INFO mapred.JobClient:     Map input bytes=2336
13/04/10 11:02:53 INFO mapred.JobClient:     Combine input records=0
13/04/10 11:02:53 INFO mapred.JobClient:     Map output records=17
13/04/10 11:02:53 INFO mapred.JobClient:     Reduce input record=17



你可能感兴趣的:(hadoop运行java类)