搭建hadoop2的开发环境并做一个小案例

在linux下直接进行开发

在windows下使用ant开发

ant官网地址:

http://ant.apache.org/

配置系统环境变量

 

ANT_HOME       E:\hadoop\apache-ant-1.9.10-bin\apache-ant-1.9.10\

CLASSPATH       ;%ANT_HOME%lib; 

PATH     ;%ANT_HOME%bin; #验证ant:ant -version

 

用eclipse 开发hadoop工程

依赖jar包:

Hadoop官网下载:http://hadoop.apache.org/

解压后找到share里面的所有jar都需要用到

---share

---common包下所有/以及common/lib包下所有jar包

---hdfs包下及hdfs/lib包下所有

--httpfsà/tomcat/lib包下所有

---mapreduce包下所有及mapreduce/lib包下所有jar

---tool/lib下所有jar

---yarn及yarn/lib包下所有jar

用Eclipse创建java projecet

新建lib文件夹---》复制粘贴以上所有依赖包---》右键 :build path

package test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

public class Test {
	public static void main(String[] args) throws IOException, URISyntaxException {
		final FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.122:9000"),new Configuration());
//		url:链接hdfs集群的一个路径;
		System.out.println(fileSystem);
		System.out.println("测试ant");//在控制台上输出
	}
}

其中 url------->访问hdfs的namenode地址

在项目根目录下新建build.xml-----》ant的执行文件

 

 

<?xml version="1.0" encoding="utf-8"?>
	<project name="test" default="sshexec" basedir=".">  <!-- 项目名,default的值是对应下面默认执行的target(任务) -->  
	    <description>  
	        simple example build file  
	    </description>  
	  <!-- set global properties for this build -->  
	  <property name="project" location="test"/>  
	  <property name="src" location="${basedir}/src"/> <!-- 设置变量,指向要编译的java代码的位置 -->  
	  <property name="lib.dir" location="${basedir}/lib"/> <!-- 设置变量,指向所依赖的jar包所在的位置 -->  
	  <property name="build" location="${basedir}/build"/> <!-- 设置变量,指向编译后的class文件的位置 -->  
	  <property name="dist"  location="${basedir}/dist"/> <!-- 设置变量,指向编译后生成jar包的位置 -->  
	  <property name="localpath.dir" value="${basedir}"/>
	  <property name="remote.home" value="~"/>
	  <!-- 可以修改 -->
	  <property name="remote.hostname" value="hadoop1"/>
	  <!-- 可以修改 -->
	  <property name="remote.username" value="root"/>
	  <!-- 可以修改 -->
	  <property name="remote.password" value="root"/>
	  <!-- 可以修改:每次需要知道的main类,写到这里 -->
	  <property name="main.class" value="test.Test"/>
	  <!-- 设置要依赖的jar包规则 -->  
	  <path id="project.class.path">    
	        <pathelement path="${build}" />    
	        <fileset dir="${lib.dir}">    
	            <include name="**/*.jar" />    
	        </fileset>    
	    </path>  
		<target name="clean"  
			        description="clean up" >  
			    <!-- Delete the ${build} and ${dist} directory trees -->  
			    <delete dir="${build}"/>  
			    <delete dir="${dist}"/>  
			  </target>  
	  <target name="init" depends="clean">  
	    <!-- Create the time stamp -->  
	    <tstamp/>  
	    <!-- Create the build directory structure used by compile -->  
	    <mkdir dir="${build}"/>
	  	<!-- Create the distribution directory -->  
	    <mkdir dir="${dist}"/>  
	  </target>  
	  
	  <target name="compile" depends="init"  
	        description="compile the source " >  
	    <!-- Compile the java code from ${src} into ${build} -->  <!-- 编译的版本不能比运行的版本高 -->
	    <javac srcdir="${src}" destdir="${build}" fork="true" executable="F:\jdk1.6.0_45\bin\javac"
	    	includeantruntime="on" classpath="${lib}">  
	        <classpath refid="project.class.path" /> <!-- 引入依赖的jar包 -->  
	    </javac>  
	  </target>  
	  
	  <target name="dist" depends="compile"  
	        description="generate the distribution" >  
	    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->  
	    <jar jarfile="${dist}/jar.jar" basedir="${build}"/>  <!-- 配置生成的jar包的路径 -->  
	  </target>  
	  
	   
		<!-- 上传到服务器 
			**需要把lib目录下jsch-0.1.15拷贝到$ANT_HOME/lib下,如果Eclipse下的ant环境必须在Windows->preference->ANT->RUNIIME->CLASSPATH中加入jscn-0.1.15.
			-->
			<target name="ssh" depends="dist">
				<scp file="${dist}/jar.jar" todir="${remote.username}@${remote.hostname}:${remote.home}" password="${remote.password}" trust="true"/>
			</target>
			<target name="sshexec" depends="ssh">
				<sshexec command="/home/hadoop/hadoop-2.6.5/bin/hadoop jar ${remote.home}/jar.jar ${main.class}" host="${remote.hostname}" username="${remote.username}" password="${remote.password}" trust="true"  />
			</target>
	</project>  
	<project name="test" default="sshexec" basedir=".">  <!-- 项目名,default的值是对应下面默认执行的target(任务) -->  
	    <description>  
	        simple example build file  
	    </description>  
	  <!-- set global properties for this build -->  
	  <property name="project" location="test"/>  
	  <property name="src" location="${basedir}/src"/> <!-- 设置变量,指向要编译的java代码的位置 -->  
	  <property name="lib.dir" location="${basedir}/lib"/> <!-- 设置变量,指向所依赖的jar包所在的位置 -->  
	  <property name="build" location="${basedir}/build"/> <!-- 设置变量,指向编译后的class文件的位置 -->  
	  <property name="dist"  location="${basedir}/dist"/> <!-- 设置变量,指向编译后生成jar包的位置 -->  
	  <property name="localpath.dir" value="${basedir}"/>
	  <property name="remote.home" value="~"/>
	  <!-- 可以修改 -->
	  <property name="remote.hostname" value="hadoop1"/>
	  <!-- 可以修改 -->
	  <property name="remote.username" value="root"/>
	  <!-- 可以修改 -->
	  <property name="remote.password" value="root"/>
	  <!-- 可以修改:每次需要知道的main类,写到这里 -->
	  <property name="main.class" value="test.Test"/>
	  <!-- 设置要依赖的jar包规则 -->  
	  <path id="project.class.path">    
	        <pathelement path="${build}" />    
	        <fileset dir="${lib.dir}">    
	            <include name="**/*.jar" />    
	        </fileset>    
	    </path>  
		<target name="clean"  
			        description="clean up" >  
			    <!-- Delete the ${build} and ${dist} directory trees -->  
			    <delete dir="${build}"/>  
			    <delete dir="${dist}"/>  
			  </target>  
	  <target name="init" depends="clean">  
	    <!-- Create the time stamp -->  
	    <tstamp/>  
	    <!-- Create the build directory structure used by compile -->  
	    <mkdir dir="${build}"/>
	  	<!-- Create the distribution directory -->  
	    <mkdir dir="${dist}"/>  
	  </target>  
	  
	  <target name="compile" depends="init"  
	        description="compile the source " >  
	    <!-- Compile the java code from ${src} into ${build} -->  <!-- 编译的版本不能比运行的版本高 -->
	    <javac srcdir="${src}" destdir="${build}" fork="true" executable="F:\jdk1.6.0_45\bin\javac"
	    	includeantruntime="on" classpath="${lib}">  
	        <classpath refid="project.class.path" /> <!-- 引入依赖的jar包 -->  
	    </javac>  
	  </target>  
	  
	  <target name="dist" depends="compile"  
	        description="generate the distribution" >  
	    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->  
	    <jar jarfile="${dist}/jar.jar" basedir="${build}"/>  <!-- 配置生成的jar包的路径 -->  
	  </target>  
	  
	   
		<!-- 上传到服务器 
			**需要把lib目录下jsch-0.1.15拷贝到$ANT_HOME/lib下,如果Eclipse下的ant环境必须在Windows->preference->ANT->RUNIIME->CLASSPATH中加入jscn-0.1.15.
			-->
			<target name="ssh" depends="dist">
				<scp file="${dist}/jar.jar" todir="${remote.username}@${remote.hostname}:${remote.home}" password="${remote.password}" trust="true"/>
			</target>
			<target name="sshexec" depends="ssh">
				<sshexec command="/home/hadoop/hadoop-2.6.5/bin/hadoop jar ${remote.home}/jar.jar ${main.class}" host="${remote.hostname}" username="${remote.username}" password="${remote.password}" trust="true"  />
			</target>
	</project>  

 

运行的时候---》右键build.xml--->run as--->Ant Build

执行结果如下:

 

最后输出的是fileSystem的信息:

 [sshexec] DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_-588939364_1, ugi=root (auth:SIMPLE)]]

 [sshexec] 娴嬭瘯ant  #乱码了

 

配置一下eclipse 中关于ant d ANT_HOME路径为设置本地的ant路径

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值