环境配置
首先需要在hadoop-env.sh中配置 HADOOP_CLASSPATH变量..
export HADOOP_CLASSPATH=/home/hadoop/hadoop-1.2.1/myclass
在.bash_profile中配置JAVA_HOME ,并且生效
测试程序
URLCat.java import org.apache.hadoop.fs.FsUrlStreamHandlerFactory; import org.apache.hadoop.io.IOUtils; import java.io.InputStream; import java.net.URL; public class URLCat { static { URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory()); } public static void main(String[] args) throws Exception { InputStream in = null; try { in = new URL(args[0]).openStream(); IOUtils.copyBytes(in, System.out, 4096, false); } finally { IOUtils.closeStream(in); } } } javac -classpath ../hadoop-core-1.2.1.jar URLCat.java hadoop URLCat hdfs://hadoop1:9000/user/hadoop/inp/test2.txt
通过引入classpath和import可以使用这个URLCat,通过hdfs的API读取文件.
API在线手册: http://hadoop.apache.org/docs/r1.2.1/api/index.html
使用ant
它也是apache的一个编译软件, 也是用于编译的. 完成工程项目的编译..
下载地址:http://ant.apache.org/bindownload.cgi
wget http://mirror.bit.edu.cn/apache//ant/binaries/apache-ant-1.9.4-bin.tar.gz #下载参考代码.. wget -SO 11101.zip http://www.packtpub.com/code_download/11101 unzip 11101.zip cd 7287OS_Code/7287OS_Code/chapter2 ls -l total 8 drwxrwxr-x 2 hadoop hadoop 4096 Apr 2 18:07 HDFS_C_API drwxrwxr-x 3 hadoop hadoop 4096 Apr 2 18:07 HDFS_Java_API cd HDFS_Java_API/
export HADOOP_HOME=/home/hadoop/hadoop-1.2.1 #建议放到.bash_profile /home/hadoop/apache-ant-1.9.4/bin/ant #进行编译.. #在当前目录下会生成一个HDFSJavaAPI.jar #在build目录中是class文件 #执行API的jar包,进行测试. hadoop jar HDFSJavaAPI.jar HDFSJavaAPIDemo
时间: 2024-10-27 01:31:31