hadoop:通过Configuration读取hdfs

package tju;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadFile2 {

    private static FileSystem getFileSystem() throws IOException {
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS", "hdfs://xx.xx.xx.xx:9000/");
        FileSystem fileSystem = FileSystem.get(configuration);
        return fileSystem;
    }

    private static void readFileFromHdfs(String filePath){
        FSDataInputStream fsDataInputStream = null;
        try {
            Path path = new Path(filePath);
            fsDataInputStream = getFileSystem().open(path);
            IOUtils.copyBytes(fsDataInputStream, System.out, 4096, false);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fsDataInputStream != null){
                IOUtils.closeStream(fsDataInputStream);
            }
        }

    }

    private static void writeFileToHdfs(String localPath, String hdfsPath){
        FSDataOutputStream outputStream = null;
        FileInputStream fileInputStream = null;

        try {
            Path path = new Path(hdfsPath);
            outputStream =getFileSystem().create(path);
            fileInputStream = new FileInputStream(new File(localPath));
            IOUtils.copyBytes(fileInputStream, outputStream,4096, false);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileInputStream != null){
                IOUtils.closeStream(fileInputStream);
            }
            if(outputStream != null){
                IOUtils.closeStream(outputStream);
            }
        }

    }

    public static void main(String[] args) {


        //String localPath= "/home/liuzhendong/userinfo.txt";
        //String hdfsPath = "hdfs://localhost:9000/input/userinfo.txt";
        //writeFileToHdfs(localPath, hdfsPath);


        String filePath = "/tju/userinfo.txt";
        readFileFromHdfs(filePath);




    }


}

你可能感兴趣的:(Hadoop,hadoop,hdfs,大数据)