Environment configuration
You first need to configure the Hadoop_classpath variable in hadoop-env.sh:
Export Hadoop_classpath=/home/hadoop/hadoop-1.2.1/myclass
Configure Java_home in. Bash_profile and take effect
Test program
Urlcat.javaimport 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&NBSP, .... /hadoop-core-1.2.1.jar urlcat.java hadoop urlcat hdfs://hadoop1:9000/usEr/hadoop/inp/test2.txt
By introducing Classpath and import, you can use this urlcat to read files through the HDFs API.
API Online Brochure: http://hadoop.apache.org/docs/r1.2.1/api/index.html
Using Ant
It is also a compile software for Apache, and it is also used for compiling. Complete the project compilation.
: http://ant.apache.org/bindownload.cgi
wget http://mirror.bit.edu.cn/apache//ant/binaries/apache-ant-1.9.4-bin.tar.gz# Download Reference code: Wget-so 11101.zip http://www.packtpub.com/code_download/11101unzip 11101.zipcd 7287os_code/7287os_code/chapter2ls- L Total 8drwxrwxr-x 2 Hadoop hadoop 4096 Apr 2 18:07 hdfs_c_apidrwxrwxr-x 3 Hadoop hadoop 4096 Apr 2 18:07 HDFS_JAVA_APICD 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# in the build directory is the class file # Execute API Jar package for testing. Hadoop jar Hdfsjavaapi.jar Hdfsjavaapidemo
This article is from the "Xiaofeng Moon" blog, make sure to keep this source http://kinda22.blog.51cto.com/2969503/1582559
The Hdfs-javaapi of Hadoop