eclipse下开发hadoop有个hadoop2x-eclipse-plugin插件,可以帮助我们在hadoop集群上运行调试我们的程序,其项目地址为https://github.com/winghc/hadoop2x-eclipse-plugin。
我下载源码包后发现其release包中只有hadoop 2.6的,我本机的环境是hadoop 2.7.1 所以需要自己编译
其官方的编译指导文件描述如下
[hdpusr@demo hadoop2x-eclipse-plugin]$ cd src/contrib/eclipse-plugin
# Assume hadoop installation directory is /usr/share/hadoop
[hdpusr@apclt eclipse-plugin]$ ant jar -Dversion=2.4.1 -Dhadoop.version=2.4.1 -Declipse.home=/opt/eclipse -Dhadoop.home=/usr/share/hadoop
final jar will be generated at directory
${hadoop2x-eclipse-plugin}/build/contrib/eclipse-plugin/hadoop-eclipse-plugin-2.4.1.jar
可以看出使用的是ant编译,我在 hadoop的源码编译中已经安装了ant,所以并不需要安装直接执行"ant jar -Dversion=2.4.1 -Dhadoop.version=2.4.1 -Declipse.home=/opt/eclipse -Dhadoop.home=/usr/share/hadoop"命令就好,其中:-Dversion -Dhadoop.version都是hadoop的版本号;-Declipse.home指向eclipse根目录;-Dhadoop.home指向hadoop根目录,例如我的就是
[hadoop@Panda-CentOS Downloads]$ cd hadoop2x-eclipse-plugin-master [hadoop@Panda-CentOS hadoop2x-eclipse-plugin-master]$ cd src/contrib/eclipse-plugin/ [hadoop@Panda-CentOS eclipse-plugin]$ pwd /home/hadoop/Downloads/hadoop2x-eclipse-plugin-master/src/contrib/eclipse-plugin [hadoop@Panda-CentOS eclipse-plugin]$ ant jar -Dversion=2.7.1 -Dhadoop.version=2.7.1 -Declipse.home=/usr/local/eclipse-hadoop/ -Dhadoop.home=/home/hadoop/hadoop/2.7.1/ -debug注意:其中加上 -debug是为了查看ant过程中的调试信息,因为原始项目中是为hadoop 2.6配置的,编译中肯定有错误,一定要加上-debug,要不然编译出错都不知道为什么
编译过程中会出现以下错误
BUILD FAILED /home/hadoop/Downloads/hadoop2x-eclipse-plugin-master/src/contrib/eclipse-plugin/build.xml:121: Warning: Could not find file /home/hadoop/hadoop/2.7.1/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar to copy. at org.apache.tools.ant.taskdefs.Copy.copySingleFile(Copy.java:642) at org.apache.tools.ant.taskdefs.Copy.execute(Copy.java:458) at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293) at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106) at org.apache.tools.ant.Task.perform(Task.java:348) at org.apache.tools.ant.Target.execute(Target.java:435) at org.apache.tools.ant.Target.performTasks(Target.java:456) at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405) at org.apache.tools.ant.Project.executeTarget(Project.java:1376) at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) at org.apache.tools.ant.Project.executeTargets(Project.java:1260) at org.apache.tools.ant.Main.runBuild(Main.java:853) at org.apache.tools.ant.Main.startAnt(Main.java:235) at org.apache.tools.ant.launch.Launcher.run(Launcher.java:285) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:112)这就是上面说的原始工程是以hadoop 2.6配置的,而hadoop 2.7.1中依赖的jar包和hadoop 2.6不同,所以找不到要拷贝的jar包。我们可以在${hadoop_home}/share/hadoop/common/lib下找到2.7.1下的jar版本,同时根据官方文档,修改配置文件${hadoop2x-eclipse-plugin-master}/ivy/libraries.properties
#htrace.version=3.0.4 htrace.version=3.1.0-incubating ... #slf4j-api.version=1.7.5 #slf4j-log4j12.version=1.7.5 slf4j-api.version=1.7.10 slf4j-log4j12.version=1.7.10其中#注释掉的是原版本,后面追加的是新版本
再次运行
[hadoop@Panda-CentOS eclipse-plugin]$ ant jar -Dversion=2.7.1 -Dhadoop.version=2.7.1 -Declipse.home=/usr/local/eclipse-hadoop/ -Dhadoop.home=/home/hadoop/hadoop/2.7.1/ -debug就可以编译成功了
如果把编译后的jar包(例如我的是hadoop-eclipse-plugin-2.7.1.jar)放到eclipse的plugins文件夹下没有效果,可以重启eclipse,在启动eclipse的命令后面加上-clean