HDFS Java API 에서 사용 하 는 파일 내용 읽 기

2814 단어 Hadoop
package com.ibeifeng.hadoop.senior.hdfs;

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;

public class HdfsApp {
    public static FileSystem getFileSystem() throws Exception{
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(conf);
        return fileSystem;
    }

    public static void readFile(String filename) throws Exception{
                //get filesystem
                FileSystem fileSystem = getFileSystem();
                //String filename = "/user/beifeng/mapreduce/input/word";
                //read path
                //String name = filename;
                Path readPath = new Path(filename);
                //open file
                FSDataInputStream inStream = fileSystem.open(readPath);

                //
                try{
                    IOUtils.copyBytes(inStream, System.out, 4096, false);
                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    //close Stream
                    IOUtils.closeStream(inStream);
                }
            }



    public static void main(String[] args) throws Exception {

        String filename = "/user/beifeng/mapreduce/input/word";
        readFile(filename);

}
    }

좋은 웹페이지 즐겨찾기