编译hadoop-eclipse插件并测试

由于开发人员使用不同的eclipse版本,hadoop自0.20.x版本后不再提供现成的hadoop-eclipse插件而是给出了源码自行编译。

一、环境和所需软件

1、ubuntu 12.04

2、eclipse-4.2

3、ant-1.8.4

4、hadoop-1.1.1

5、由于需要对hadoop native库进行编译,需要安装automake autoconf libtool

sudo apt-get install automake autoconf libtool

二、编译hadoop-eclipse-plugin-1.1.2.jar插件

(一)、building hadoop

1、编辑{HADOOP_HOME}/build.xml

(1)、对31行的hadoop版本做修改

<property name="version"value="1.1.2-SNAPSHOT"/>

修改为:

<property name="version"value="1.1.2"/>


(2)、2418行的ivy下载进行注释,因为已经包含了ivy.jar

  <!--target name="ivy-download"description="To download ivy"unless="offline">
   <get src="${ivy_repo_url}" dest="${ivy.jar}"usetimestamp="true"/>
  </target-->

(3)、2426行去除对ivy-download的依赖关系,保留如下
    
<target name="ivy-init-antlib"depends="ivy-init-dirs,ivy-probe-antlib"

2、 编辑 {HADOOP_HOME}/src/contrib./build-contrib.xml

添加红色字体的两行,补充Eclipse路径和Hadoop版本

<projectname="hadoopbuildcontrib" xmlns:ivy="antlib:org.apache.ivy.ant">

 

  <propertyname="eclipse.home"location="eclipse的安装目录"/>

  <propertyname="version"value="1.1.2"/>   //build的hadoop的版本号

  <propertyname="name"value="${ant.project.name}"/>

  <propertyname="root"value="${basedir}"/>

  <propertyname="hadoop.root"location="${root}/../../../"/>

...

</project>

3、building hadoop

cd ${HADOOP-HOME}

ant compile


(二)、building eclipse-plugin for hadoop

1、修改${HADOOP_HOME}/src/contrib/eclipse-plugin/build.xml

加入下面红色字体的几行,指定添加的jar包:

<!-- Override jar target to specify manifest -->
  <target name="jar" depends="compile" unless="skip.contrib">
    <mkdir dir="${build.dir}/lib"/>
    <copy file="${hadoop.root}/build/hadoop-core-${version}.jar" tofile="${build.dir}/lib/hadoop-core.jar" verbose="true"/>
    <copy file="${hadoop.root}/build/ivy/lib/Hadoop/common/commons-cli-${commons-cli.version}.jar"  todir="${build.dir}/lib" verbose="true"/>
   <copy file="${hadoop.root}/lib/commons-configuration-1.6.jar" tofile="${build.dir}/lib/commons-configuration-1.6.jar" verbose="true"/>
   <copy file="${hadoop.root}/lib/commons-httpclient-3.0.1.jar" tofile="${build.dir}/lib/commons-httpclient-3.0.1.jar" verbose="true"/>
   <copy file="${hadoop.root}/lib/commons-lang-2.4.jar" tofile="${build.dir}/lib/commons-lang-2.4.jar" verbose="true"/>
   <copy file="${hadoop.root}/lib/jackson-core-asl-1.8.8.jar" tofile="${build.dir}/lib/jackson-core-asl-1.8.8.jar" verbose="true"/>
   <copy file="${hadoop.root}/lib/jackson-mapper-asl-1.8.8.jar" tofile="${build.dir}/lib/jackson-mapper-asl-1.8.8.jar" verbose="true"/>    

<jar
      jarfile="${build.dir}/hadoop-${name}-${version}.jar"
      manifest="${root}/META-INF/MANIFEST.MF">
      <fileset dir="${build.dir}" includes="classes/ lib/"/>
      <fileset dir="${root}" includes="resources/ plugin.xml"/>
 </jar>
  </target>

2、修改${HADOOP_HOME}/src/contrib/eclipse-plugin/META-INF/MENIFEST.MF,指定CLASS_PATH

Bundle-ClassPath: classes/,
 lib/hadoop-core.jar,lib/commons-configuration-1.6.jar,lib/commons-httpclient-3.0.1.jar,lib/commons-lang-2.4.jar,lib/jackson-core-asl-1.8.8.jar,lib/jackson-mapper-asl-1.8.8.jar,lib/commons-cli-1.2.jar

3、building

cd ${HADOOP_HOME}/usr/contrib/eclipse-plugin

ant jar

building输出的eclipse插件:${HADOOP_HOME}/build/contrib/eclipse-plugin/hadoop-eclipse-plugin-1.1.2.jar

三、eclipse配置

 1、 打开Window-->Preferens,你会发现HadoopMap/Reduce选项,在这个选项里你需要配置Hadoop installation directory。配置完成后退出。

2、选择window -> open perspective -> Other..., 选择有大象图标的 Map/Reduce.


此时,就打开了Map/Reduce的开发环境。可以看到,右下角多了一个Map/Reduce Locations的框。如下图:

3、设置Hadoop的环境参数。选择Map/ReduceLocations标签,点击该标签最右边的大象图标,即那个齿轮状图标右侧的大象图标,打开参数设置页面,参数填写如下图:


LocationName :此处为参数设置名称,可以任意填写

Map/ReduceMaster (此处为Hadoop集群的Map/Reduce地址,应该和mapred-site.xml中的mapred.job.tracker设置相同)

    Host: 10.0.0.211

    port: 9001

DFSMaster  (此处为Hadoop的master服务器地址,应该和core-site.xml中的fs.default.name 设置相同)

    Host: 10.0.0.211

    Port: 9000

设置完成后,点击Finish就应用了该设置。

此时,在最左边的ProjectExplorer中就能看到DFS的目录,如下图所示。

注:解决linux上运行权限的问题,可以在服务器创建一个和hadoop集群用户名一致的用户,即可不用修改master的permissions策略。


四、测试

1、 新建项目:File-->New-->Other-->Map/Reduce Project ,项目名可以随便取,如HadoopTest

2、将测试代码 src.zip解压到HadoopTest源码目录的src目录下,刷新项目源码目录结构如下:

3、修改WordCountTest.java

public static void main(String[] args) throws Exception {
        // Add these statements. XXX
        File jarFile = EJob.createTempJar("bin");
        EJob.addClasspath("/opt/hadoop-1.1.1/conf");
        ClassLoader classLoader = EJob.getClassLoader();
        Thread.currentThread().setContextClassLoader(classLoader);

        String inputPath = "hdfs://localhost:9000/user/chenym/input/file*";
        String outputPath = "hdfs://localhost:9000/user/chenym/output2";


        Configuration conf = new Configuration();
        String[] otherArgs = new String[] { inputPath, outputPath };
        if (otherArgs.length != 2) {
            System.err.println("Usage: wordcount <in> <out>");
            System.exit(2);
        }

        Job job = new Job(conf, "word count");
        // And add this statement. XXX
        ((JobConf) job.getConfiguration()).setJar(jarFile.toString());
        job.setJarByClass(WordCountTest.class);
        
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        
        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

4、run as mapreduece project





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值