美文网首页HDFS
java springboot中 下载上传hdfs文件

java springboot中 下载上传hdfs文件

作者: smartjiang | 来源:发表于2020-01-03 13:43 被阅读0次

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

    相关文章

      网友评论

        本文标题:java springboot中 下载上传hdfs文件

        本文链接:https://www.haomeiwen.com/subject/wzyzoctx.html