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

    用放荡不羁的心态过随遇而安的生活
  • 相关阅读:
    构造函数的特点
    HashMap源码分析
    DVWA-7.1 SQL Injection(SQL注入)-Low
    DVWA-6.4 Insecure CAPTCHA(不安全的验证码)-Impossible
    DVWA-6.3 Insecure CAPTCHA(不安全的验证码)-High
    DVWA-6.2 Insecure CAPTCHA(不安全的验证码)-Medium
    DVWA-6.1 Insecure CAPTCHA(不安全的验证码)-Low
    DVWA-5.4 File Upload(文件上传)-Impossible
    DVWA-5.3 File Upload(文件上传)-High-绕过文件类型限制
    DVWA-5.2 File Upload(文件上传)-Medium-绕过文件类型限制
  • 原文地址:https://www.cnblogs.com/re-myself/p/5527264.html
Copyright © 2020-2023  润新知