readFile




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();
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM