1 ant准备:
a) ant安装在window环境下,并在lib下增加包: jsch-0.1.51.jar(没有此包,此包见附件 在hadoop2下回报错)
b) 在window环境下运行会依赖很多hadoop2的环境,因此建议使用ant打包部署到hadoop2集群中打包运行
c) 在window环境变量中 增加ant的配置路径
2 准备jar:
下载hadoop-2.4.0.tar.gz 解压后,新建java工程,后拷贝hadoop-2.4.0\share\hadoop内的所有jar包到工程中
3 准备ant 的xml
粘贴附件的build.xml 注意在build.xml里增加<classpath refid="compile.classpath" /> 防止编码错误
参看文章: Ant 出现警告 编码GBK 的不可映射字符
build.xml 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 该文件与src文件夹、lib文件夹同一级 -->
<project name="hadoop2测试项目" basedir="." default="sshexec">
<!--属性设置-->
<property environment="env" />
<property file="build.properties" />
<property name="src.dir" value="${basedir}/src" />
<property name="java.lib.dir" value="${env.JAVA_HOME}/lib" />
<property name="classes.dir" value="${basedir}/classes" />
<property name="dist.dir" value="${basedir}/dist" />
<property name="project.lib.dir" value="${basedir}/lib" />
<property name="localpath.dir" value="${basedir}" />
<property name="remote.home" value="~"/>
<!--可以修改:hadoop集群的hostname或者ip-->
<property name="remote.hostname" value="h2single511-115"/>
<!--可以修改:登录hadoop集群所在linux的用户名-->
<property name="remote.username" value="root"/>
<!--可以修改:登录hadoop集群所在liniux的密码-->
<property name="remote.password" value="123456"/>
<!--可以修改:每次需要运行的main类,写到这里。运行时拼接为hadoop jar xxx.jar MainClass -->
<property name="main.class" value="hdfs.App1"/>
<!--可以修改:hadoop集群在linux的部署路径-->
<property name="hadoop.path" value="/usr/local/hadoop2.4"/>
<!-- 基本编译路径设置 -->
<path id="compile.classpath">
<fileset dir="${java.lib.dir}">
<include name="tools.jar" />
</fileset>
<fileset dir="${project.lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<!-- 运行路径设置 -->
<path id="run.classpath">
<path refid="compile.classpath" />
<pathelement location="${classes.dir}" />
</path>
<!-- 清理,删除临时目录 -->
<target name="clean" description="清理,删除临时目录">
<!--delete dir="${build.dir}" /-->
<delete dir="${dist.dir}" />
<delete dir="${classes.dir}" />
<echo level="info">清理完毕</echo>
</target>
<!-- 初始化,建立目录,复制文件 -->
<target name="init" depends="clean" description="初始化,建立目录,复制文件">
<mkdir dir="${classes.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- 编译源文件-->
<target name="compile" depends="init" description="编译源文件">
<javac srcdir="${src.dir}" destdir="${classes.dir}" source="1.6" target="1.6" includeAntRuntime="false">
<classpath refid="compile.classpath" />
<compilerarg line="-encoding UTF-8 "/>
</javac>
</target>
<!-- 打包类文件 -->
<target name="jar" depends="compile" description="打包类文件">
<jar jarfile="${dist.dir}/jar.jar">
<fileset dir="${classes.dir}" includes="**/*.*" />
</jar>
</target>
<!--上传到服务器
**需要把lib目录下的jsch-0.1.51拷贝到$ANT_HOME/lib下,如果是Eclipse下的Ant环境必须在Window->Preferences->Ant->Runtime->Classpath中加入jsch-0.1.51。
-->
<target name="ssh" depends="jar">
<scp file="${dist.dir}/jar.jar" todir="${remote.username}@${remote.hostname}:${remote.home}" password="${remote.password}" trust="true"/>
</target>
<target name="sshexec" depends="ssh">
<sshexec host="${remote.hostname}" username="${remote.username}" password="${remote.password}" trust="true" command="${hadoop.path}/bin/hadoop jar ${remote.home}/jar.jar ${main.class}"/>
</target>
</project>
4 写Java代码,后在build.xml 右键 Ant Build即可
代码如下:
public class App1 {
public static void main(String[] args) throws Exception {
//http://www.baidu.com
final Configuration conf = new Configuration();
final FileSystem fileSystem = FileSystem.get(new URI("hdfs://h2single511-115:9000/"), conf);
System.out.println("**************"+fileSystem.toString());
System.out.println("**************"+fileSystem.getClass());
}
结果如下: