hadoop编程:解决eclipse能运行,打包放到集群上ClassNotFoundException:经验总结
本文之所以称之为经验,是因为我们经常碰到莫名其妙的问题,从原理上是说不通的。但是我们却确确实实碰到了。
在刚开始运行的时候,碰到了下文所表示的错误。本想作为经验总结留下来。为了让错误重现,所以又重新操作了一遍。这时候已经换环境,但是环境与原先没做任何的更改,也就是说他们的配置完全是一样的,神奇的是mapreduce执行的完全正确。对于如果碰到下面问题的同学,可以参考。对于原理性的内容,以后在更新。
对于新手,经常遇到,在eclipse中是可以运行的,但是打包放到集群上,不可行,问题如下:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/conf/Configuration
at mapreduce.mapreduce.main(mapreduce.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.conf.Configuration
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
hadoop jar '/home/user/Desktop/mapreduce.jar' mapreduce
14/03/05 05:35:09 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
14/03/05 05:35:09 WARN mapred.JobClient: No job jar file set.User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
14/03/05 05:35:09 INFO input.FileInputFormat: Total input paths to process : 1
14/03/05 05:35:09 INFO util.NativeCodeLoader: Loaded the native-hadoop library
14/03/05 05:35:10 WARN snappy.LoadSnappy: Snappy native library not loaded
14/03/05 05:35:10 INFO mapred.JobClient: Running job: job_201402280238_0011
14/03/05 05:35:11 INFO mapred.JobClient:map 0% reduce 0%
14/03/05 05:35:25 INFO mapred.JobClient: Task Id : attempt_201402280238_0011_m_000000_0, Status : FAILED
java.lang.RuntimeException: java.lang.ClassNotFoundException: mapreduce.mapreduce$MyMapper
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:867)
at org.apache.hadoop.mapreduce.JobContext.getMapperClass(JobContext.java:199)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:719)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:370)
at org.apache.hadoop.mapred.Child$4.run(Child.java:255)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1121)
at org.apache.hadoop.mapred.Child.main(Child.java:249)
Caused by: java.lang.ClassNotFoundException: mapreduce.mapreduce$MyMapper
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:820)
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:865)
... 8 more
14/03/05 05:35:31 INFO mapred.JobClient: Task Id : attempt_201402280238_0011_m_000000_1, Status : FAILED
java.lang.RuntimeException: java.lang.ClassNotFoundException: mapreduce.mapreduce$MyMapper
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:867)
at org.apache.hadoop.mapreduce.JobContext.getMapperClass(JobContext.java:199)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:719)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:370)
at org.apache.hadoop.mapred.Child$4.run(Child.java:255)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1121)
at org.apache.hadoop.mapred.Child.main(Child.java:249)
Caused by: java.lang.ClassNotFoundException: mapreduce.mapreduce$MyMapper
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:820)
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:865)
... 8 more
14/03/05 05:35:37 INFO mapred.JobClient: Task Id : attempt_201402280238_0011_m_000000_2, Status : FAILED
java.lang.RuntimeException: java.lang.ClassNotFoundException: mapreduce.mapreduce$MyMapper
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:867)
at org.apache.hadoop.mapreduce.JobContext.getMapperClass(JobContext.java:199)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:719)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:370)
at org.apache.hadoop.mapred.Child$4.run(Child.java:255)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1121)
at org.apache.hadoop.mapred.Child.main(Child.java:249)
Caused by: java.lang.ClassNotFoundException: mapreduce.mapreduce$MyMapper
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:820)
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:865)
... 8 more
14/03/05 05:35:49 INFO mapred.JobClient: Job complete: job_201402280238_0011
14/03/05 05:35:49 INFO mapred.JobClient: Counters: 7
14/03/05 05:35:49 INFO mapred.JobClient: Job Counters
14/03/05 05:35:49 INFO mapred.JobClient: SLOTS_MILLIS_MAPS=33927
14/03/05 05:35:49 INFO mapred.JobClient: Total time spent by all reduces waiting after reserving slots (ms)=0
14/03/05 05:35:49 INFO mapred.JobClient: Total time spent by all maps waiting after reserving slots (ms)=0
14/03/05 05:35:49 INFO mapred.JobClient: Launched map tasks=4
14/03/05 05:35:49 INFO mapred.JobClient: Data-local map tasks=4
14/03/05 05:35:49 INFO mapred.JobClient: SLOTS_MILLIS_REDUCES=0
14/03/05 05:35:49 INFO mapred.JobClient: Failed map tasks=1
我们需要分析上面是什么错误,其实就是找不到类,为什么会找不到类,是我们打包问题吗?是因为没有环境不正确吗?
是的,这两方面可能都有问题,那么该如何解决
首先来看看我们是如何打包的:
1.单击map.java文件,弹出快捷菜单,选择Export。
http://www.aboutyun.com/data/attachment/album/201403/08/161652uykfkkqjkvjkwln6.png
2.第一步之后,我们会看到下面,选择Java下面的JAR file。
http://www.aboutyun.com/data/attachment/album/201403/08/162056kgzqtgbotezp8cub.png
3.我们单击Next,进入JAR Export选择我们要打的包即可。
http://www.aboutyun.com/data/attachment/album/201403/08/162429o7p2sju6vj53m8zp.png
4.单击next下一步,然后在下一步,咱们会看到主类,我们选择主类到Main class
http://www.aboutyun.com/data/attachment/album/201403/08/162758ysso8ogcileaasl9.png
5.选择主类到Main class,我们会看到mapreduce.map,其实这是 包名.类型
http://www.aboutyun.com/data/attachment/album/201403/08/162759kc8j7zf424tl1j34.png
6.到这一步,单击finish。我们看到了这个包。并且看到它的大小。
http://www.aboutyun.com/data/attachment/album/201403/08/163421to3dp6godfsu6ud7.png
打包完毕,我们放到集群上运行:
出现下面情况
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/conf/Configuration
。
解决:
对于上面我们是否可以采用另外一种方法:答案是可以的。
就是我们,打成java运行jar包,是绝对可行的。
http://www.aboutyun.com/data/attachment/album/201403/08/171729ijp644ljr4rr60pz.png
我们打完包之后,我们会看到,运行包与前面包进行对比,一个K级别,一个是M级别。
http://www.aboutyun.com/data/attachment/album/201403/08/172510ud9098juc2y90l2d.png
总结:
对于上面产生的问题,通用的方法就是我们可以打包成运行包,在集群上运行是没有问题的。打成运行包,是我们把程序中相关的包都打进运行包中,虽然有点冗余,可以解决初学者面临的这个问题。
但是不适用开发环境和运行环境,版本不一致的情况
有点类似windows的LIB和DLL的味道~ 今天也遇到这种情况,在eclipse上无法运行,而在打包后就没问题了,eclipse错误提示:
15/03/28 10:47:42 INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=
15/03/28 10:47:42 WARN mapred.JobClient: No job jar file set.User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
15/03/28 10:47:42 INFO input.FileInputFormat: Total input paths to process : 1
15/03/28 10:47:42 INFO mapred.JobClient: Running job: job_local_0001
15/03/28 10:47:42 INFO input.FileInputFormat: Total input paths to process : 1
15/03/28 10:47:43 INFO mapred.MapTask: io.sort.mb = 100
15/03/28 10:47:43 WARN mapred.LocalJobRunner: job_local_0001
java.lang.OutOfMemoryError: Java heap space
at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.<init>(MapTask.java:781)
at org.apache.hadoop.mapred.MapTask$NewOutputCollector.<init>(MapTask.java:524)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:613)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:305)
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:177)
15/03/28 10:47:43 INFO mapred.JobClient:map 0% reduce 0%
15/03/28 10:47:43 INFO mapred.JobClient: Job complete: job_local_0001
15/03/28 10:47:43 INFO mapred.JobClient: Counters: 0
mark,学习了
其实还有设置配置文件之后,打jar包读取读取不到的情况,这时候就要把配置文件复制到jar包的跟路径下就可以了 mc123612997 发表于 2016-4-5 19:09
其实还有设置配置文件之后,打jar包读取读取不到的情况,这时候就要把配置文件复制到jar包的跟路径下就可以 ...
配置文件?能否详细说说,为啥会这样
Alkaloid0515 发表于 2016-4-5 19:12
配置文件?能否详细说说,为啥会这样
一般我们的配置文件都在resources这个文件夹里边,和src平级,但是打成jar包的时候,jar不会读取,所以我就经常手动移配置文件的位置了
页:
[1]