环境配置 首先需要在hadoop-env.sh中配置 HADOOP_CLASSPATH变量.. 1
| export HADOOP_CLASSPATH=/home/hadoop/hadoop-1.2.1/myclass
|
在.bash_profile中配置JAVA_HOME ,并且生效
测试程序 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| 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的一个编译软件, 也是用于编译的. 完成工程项目的编译..
1
2
3
4
5
6
7
| 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
|
|