• 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会抛出异常。

    用放荡不羁的心态过随遇而安的生活
  • 相关阅读:
    书单
    parzen 窗的matlab实现
    Oracle-07
    图片加载不出来
    省市区三表建立及数据插入
    a链接与button按钮混用
    省市区三级联动,获取地区编号,JSON格式
    纯JS省市区三级联动
    JDK与JRE的区别
    web-03-jQuery
  • 原文地址:https://www.cnblogs.com/re-myself/p/5527264.html
Copyright © 2020-2023  润新知