環境:Hadoop1.2.1 java
例:直接使用FileSystem以標準輸出格式顯示Hadoop文件系統中的文件
編寫JAVA文件 shell
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import java.io.InputStream; import java.net.URI; public class FileSystemCat { public static void main(String[] args) throws Exception { String uri = args[0]; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri), conf); InputStream in = null; try { in = fs.open(new Path(uri)); IOUtils.copyBytes(in, System.out, 4096, false); } finally { IOUtils.closeStream(in); } } }設置JAVA_HOME環境變量,將$JAVA_HOME/bin添加到PATH環境變量
[grid@hadoop1 hadoop-1.2.1]# vi conf/hadoop-env.sh # The java implementation to use. Required. export JAVA_HOME=/usr/java/jdk1.7.0_72 # Extra Java CLASSPATH elements. Optional. export HADOOP_CLASSPATH=/home/grid/hadoop-1.2.1/myclasses
上傳編輯好的JAVA文件 apache
[grid@hadoop1 hadoop-1.2.1]$ mkdir myclasses [grid@hadoop1 hadoop-1.2.1]$ cd myclasses [grid@hadoop1 myclasses]$ rz
編譯java文件 oop
[grid@hadoop1 myclasses]$ javac -classpath ../hadoop-core-1.2.1.jar FileSystemCat.java
執行: ui
[grid@hadoop1 hadoop-1.2.1]$ ./bin/hadoop FileSystemCat hdfs://hadoop1:9000/user/grid/in/test3.txt Hello Hadoop Java API