import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; /*** * * @author * */ public class readFile { public static void main(String[] args) throws IOException { //創建Configuration實例 Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://192.168.1.100:9000"); //通過配置獲取到文件系統 FileSystem fs = FileSystem.get(conf); //定義要讀取文件所在的HDFS路徑 Path src=new Path("hdfs://192.168.1.100:9000/user/hadoop/input/core-site.xml"); //通過文件系統的open()方法得到一個文件輸入流,用於讀取 FSDataInputStream dis = fs.open(src); //用IOUtils下的copyBytes將流中的數據打印輸出到控制台 IOUtils.copyBytes(dis, System.out, conf); //關閉輸入流 dis.close(); } }