HDFS

java springboot中 下载上传hdfs文件

2020-01-03  本文已影响0人  smartjiang

在java springboot 中读取hdfs上面文件,首先在src/resources中添加hdfs-site.xml文件,下面为读取示例:

public class HdfsFileUtil {


    public void writeHDFS(String localPath, String hdfsPath){
        FSDataOutputStream outputStream = null;
        FileInputStream fileInputStream = null;

        try {
            Path path = new Path(hdfsPath);
            outputStream = this.getFiledSystem().create(path);
            fileInputStream = new FileInputStream(new File(localPath));
            //输入流、输出流、缓冲区大小、是否关闭数据流,如果为false就在 finally里关闭
            IOUtils.copyBytes(fileInputStream, outputStream,4096, false);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileInputStream != null){
                IOUtils.closeStream(fileInputStream);
            }
            if(outputStream != null){
                IOUtils.closeStream(outputStream);
            }
        }

    }

    private FileSystem getFiledSystem() throws IOException {
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(configuration);
        return fileSystem;
    }

    public void readHDFSFile(String filePath,String savepath){
        FSDataInputStream fsDataInputStream = null;

        try {
            Path path = new Path(filePath);
            fsDataInputStream = this.getFiledSystem().open(path);
            BufferedOutputStream bf=new BufferedOutputStream(new FileOutputStream(savepath));
            IOUtils.copyBytes(fsDataInputStream, bf, 4096, false);
            bf.flush();
            bf.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fsDataInputStream != null){
                IOUtils.closeStream(fsDataInputStream);
            }
        }

    }
}
上一篇 下一篇

猜你喜欢

热点阅读