【Hadoop】Build and Execute

Hadoop官方wiki上介绍了如何用Maven编译和生成Eclipse项目,但是它需要hadoop-common的trunk代码,因为只有trunk上有pom.xml文件。而对于其他release版本的源代码,都包含build.xml,因此可以通过ant来编译。本文根据自己的经验,总结一下如何将下载的tar.gz源码包导入Eclipse,进而通过Eclipse编译和执行Hadoop。

Compile and Build

  1. 从官网上下载源码包,这里下载的是hadoop-1.0.4.tar.gz,不需要解压
  2. 新建一个空的Eclipse Project,Import -> General -> Archive File,选择此tar.gz文件,将其全部导入新建的Project
  3. 将需要编译的源码package加入build path:Build Path -> Configure Build Path,这里加入core, examples, hdfs, mapred四个package。同时,将default output folder设置为build/classes (hadoop启动脚本默认将$HADOOP_HOME/build/classes加入classpath),如下图所示:
  4. 将lib目录下的所有jar文件加入Project Libraries,如下图所示:
  5. 接下来Eclipse将开始自动build,会发现很多编译错误。解决方法是下载一个jar文件,com.springsource.org.apache.tools.ant-1.8.1.jar,将其加入Project Library。

Execute

如果需要编写MapReduce程序,可以新建另一个Project,然后将Hadoop-1.0.4/build/classes目录加入Project Library。单元测试Mapper和Reducer可以使用MRUnit,测试代码如下所示:
	@Test
	public void testMap() {
		Text value = new Text("zhang jun");
		new MapDriver<Object, Text, Text, IntWritable>()
			.withMapper(new JunzWordCount.TokenizerMapper())
			.withInputValue(value)
			.withOutput(new Text("zhang"), ONE)
			.withOutput(new Text("jun"), ONE)
			.runTest();
	}
	
	@Test
	public void testReduce() {
		List<IntWritable> values = new ArrayList<IntWritable>();
		values.add(ONE);
		values.add(ONE);
		new ReduceDriver<Text,IntWritable,Text,IntWritable>()
			.withReducer(new JunzWordCount.IntSumReducer())
			.withInput(new Text("zhang"), values)
			.withOutput(new Text("zhang"), new IntWritable(2))
			.runTest();
	}

学习过程中可能需要修改源代码并测试执行,此时可以在Local Mode下运行Hadoop(Hadoop可以工作在三个模式,Local Mode,Pseudo-distributed, Distributed)。这里假定在JobClient.submitJobInternal()中增加一行LOG输出,这样在MapReduce Job提交的时候,就会输出该log:
  public 
  RunningJob submitJobInternal(final JobConf job
                               ) throws FileNotFoundException, 
                                        ClassNotFoundException,
                                        InterruptedException,
                                        IOException {
    /*
     * configure the command line options correctly on the submitting dfs
     */
    return ugi.doAs(new PrivilegedExceptionAction<RunningJob>() {
      public RunningJob run() throws FileNotFoundException, 
      ClassNotFoundException,
      InterruptedException,
      IOException{
    	//Add one log
    	LOG.info("Hello Junz");
Eclipse build完之后,将项目目录复制到VMware虚拟机的共享目录,在Guest CentOS下运行,可以看到输出的信息。通过查看./bin/hadoop启动脚本,可以知道它设定的classpath,$HADOOP_HOME/build/classes在$HADOOP_HOME/hadoop-core-1.0.4.jar之前,也就是说本地修改和编译的class优先被加载,因而可以看到修改后的代码反映在运行之中。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值