zoukankan      html  css  js  c++  java
  • hadoop之 HDFS读写 java interface

    读取文件

        public static void readFile(String uri) {
    
            Configuration configuration = new Configuration();
            InputStream inputStream = null;
            try {
                // String uri = "hdfs://hadoop:9000/hadoop/temp/1901";
                FileSystem fileSystem = FileSystem.get(URI.create(uri), configuration);
                inputStream = fileSystem.open(new Path(uri));
                IOUtils.copyBytes(inputStream, System.out, 4096, false);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeStream(inputStream);
            }
        }
    
    

    seek 读取

        public static void readFileByFSDATAInputStream(String uri) {
            Configuration configuration = new Configuration();
            FSDataInputStream stream = null;
            try {
                FileSystem fileSystem = FileSystem.get(URI.create(uri), configuration);
                stream = fileSystem.open(new Path(uri));
                IOUtils.copyBytes(stream, System.out, 4096, false);
    
                stream.seek(0);
                IOUtils.copyBytes(stream, System.out, 4096, false);
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeStream(stream);
            }
        }
    
    

    写文件

        public static void writeData(String uri, String local) {
    
    
            try {
                InputStream inputStream = new BufferedInputStream(new FileInputStream(local));
                FileSystem fileSystem = FileSystem.get(configuration);
    
    
                // 创建一个新文件 必须保证uri不存在  如果向一个已经存在的文件添加内容,使用fileSystem.append(Path p)
                //fileSystem.exists(new Path("")); //判断文件是否存在
                //fileSystem.mkdirs(new Path(""));  //创建文件夹
                FSDataOutputStream outputStream = fileSystem.create(new Path(uri), new Progressable() {
                    // 报告写入过程
                    @Override
                    public void progress() {
                        System.out.print(".");
                    }
                });
    
                IOUtils.copyBytes(inputStream, outputStream, 4096, true);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
        }
    
    

    列出文件列表

        public static void listFiles(String uri) {
    
            try {
                FileSystem fileSystem = FileSystem.get(configuration);
              //  FileStatus fileStatus = fileSystem.getFileStatus(new Path(uri));
                FileStatus[] statuses = fileSystem.listStatus(new Path(uri));
                Path[] listPaths = FileUtil.stat2Paths(statuses);
                for (Path path : listPaths){
                    System.out.println(path);
                }
                // fileSystem.listStatus(Path[] files); //批量读取
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
    

    删除文件

        public static void deleteFile(String uri){
    
            try {
                FileSystem fileSystem = FileSystem.get(configuration);
                fileSystem.delete(new Path(uri),true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    

    源码

     /** Delete a file.
       *
       * @param f the path to delete.
       * @param recursive if path is a directory and set to 
       * true, the directory is deleted else throws an exception. In
       * case of a file the recursive can be set to either true or false. 
       * @return  true if delete is successful else false. 
       * @throws IOException
       */
      public abstract boolean delete(Path f, boolean recursive) throws IOException;
    
    

    如果删除文件夹需要 设置为true,如果未flase会抛出异常。

    用放荡不羁的心态过随遇而安的生活
  • 相关阅读:
    没有功能需求文档就拒绝开发吗?
    用Spring cloud Stream来开发基于MQ消息驱动的微服务
    在Linux上讲Java命令行的作为服务运行
    EF提供的三种查询方式
    C#中sealed关键字
    winform Chart控件 获取鼠标处坐标值方法
    Linq to Entity中连接两个数据库时要注意的问题
    linq中查询列表的使用及iqueryable和list集合之间的转换
    C语言关键字register、extern、static
    DllImport的具体用法
  • 原文地址:https://www.cnblogs.com/re-myself/p/5527264.html
Copyright © 2011-2022 走看看