Hadoop-2.x 学习笔记(3) —— HDFS Java API

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012292754/article/details/86476847

1 获取 FileSystem 对象

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 org.junit.Test;


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

public class HdfsApp {

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

        upload();
    }

    public static FileSystem getFileSystem() throws IOException {
        Configuration conf = new Configuration();

        return FileSystem.get(conf);
    }


    public static void read(String fileName) throws IOException {
        FileSystem fileSystem = getFileSystem();
        Path path = new Path(fileName);

        FSDataInputStream inputStream = fileSystem.open(path);

        try {
            IOUtils.copyBytes(inputStream, System.out, 2048, false);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(inputStream);
        }
    }

    @Test
    public static void upload() throws IOException {
        FileSystem fileSystem = getFileSystem();

        String putFile = "/out";
        Path writePath = new Path(putFile);

        FSDataOutputStream outStream = fileSystem.create(writePath);

        FileInputStream inputStream = new FileInputStream(new File("D:/scores.txt"));

        try{
            IOUtils.copyBytes(inputStream,outStream,2048,false);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            IOUtils.closeStream(inputStream);
            IOUtils.closeStream(outStream);

        }


    }


}

猜你喜欢

转载自blog.csdn.net/u012292754/article/details/86476847