zoukankan      html  css  js  c++  java
  • Hadoop权威指南:HDFS-目录,查询文件系统,删除文件

    Hadoop权威指南:HDFS-目录,查询文件系统,删除文件

    目录

    FileSystem实例提供了创建目录的方法

    public boolean mkdirs(Path f) throws IOException

    这个方法一次性创建所有必要但还没有的父目录

    通常不需要显式创建一个目录,因为调用create()方法写入文件时会自动创建所有父目录

    查询文件系统

    文件元数据:FileStatus

    • FileStatus类封装了文件系统中文件和目录的元数据包括文件长度,块大小,副本,修改时间,所有者,权限信息
    • FileSystemgetFileStatus方法用于获取文件或目录的FileStatus对象
    • 使用exists()方法检查文件或者目录是否存在

    列出文件

    使用FileSystemlistStatus()方法

    public FileStatus[] listStatus(Path f) throws IOException
    public FileStatus[] listStatus(Path f, PathFilter filter) throws IOException
    public FileStatus[] listStatus(Path[] files) throws IOException
    public FileStatus[] listStatus(Path[] files, PathFilter filter) throws IOException
    
    • 传入的Path参数可以是一个文件,也可以是一个目录
    • 允许使用PathFilter来限制匹配的文件和目录

    显示Hadoop文件系统中一组路径的文件信息

    代码
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileStatus;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.FileUtil;
    import org.apache.hadoop.fs.Path;
    
    import java.io.IOException;
    import java.net.URI;
    
    public class ListStatus {
        public static void main(String[] args) throws IOException {
            String uri = args[0];
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(uri), conf);
    
            Path[] paths = new Path[args.length];
            for (int i=0; i < paths.length; ++i) {
                paths[i] = new Path(args[i]);
            }
            FileStatus[] status = fs.listStatus(paths);
            // stat2Path2方法将一个FileStatus对象数组转换为一个Path对象数组
            Path[] listedPaths = FileUtil.stat2Paths(status);
            for (Path p : listedPaths) {
                System.out.println(p);
            }
        }
    }
    
    编译

    javac ListStatus.java

    运行

    hadoop ListStatus hdfs://localhost:9000/user/hadoop/input hdfs://localhost:9000/user/hadoop/output

    文件模式

    Hadoop为执行通配[1]提供了两个FileSystem方法

    public FileStatus[] globStatus(Path pathPattern) throws IOException
    public FileStatus[] globStatus(Path pathPattern, PathFilter filter) throws IOException
    
    • globStatus()方法返回与其路径匹配于指定模式的所有文件的FileStatus对象数组,并按路径排序
    • PathFilter命令作为可选项可以进一步对匹配结果进行限制

    Hadoop支持的通配符与Unix bash的相同

    通配符 名称 匹配
    * 星号 匹配0或多个字符
    ? 问号 匹配单一字符
    [ab] 字符类 匹配{a,b}集合中的一个字符
    [^ab] 非字符类 匹配非{a,b}集合中的一个字符
    [a-b] 字符范围 匹配一个在a-b范围内的字符(包括a,b),a在字典顺序上要小于或等于b
    [^a-b] 非字符范围 匹配一个不在a-b范围内的字符(包括a,b),a在字典顺序上要小于或等于b
    {a,b} 或选择 匹配包含a或b中的一个的表达式
    c 转义字符 匹配元字符c

    PathFilter对象

    • 通配符模式并不总能描述我们想要访问的文件集
    • FileSystem中的listStatus()globStatus() 方法提供了可选的 PathFilter 对象, 以编程方式控制通配符
      package org.apache.hadoop.fs;
      public interface PathFilter {
        boolean accept(Path path);
      }
    
    • pathFilterjava.io.FileFilter 一样,是 Path 对象, 而不是 File 对象

    PathFilter用于排除匹配正则表达式的路径

    代码
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.fs.PathFilter;
    
    public class RegexExcludePathFilter implements PathFilter {
    
        private final String regex;
    
        public RegexExcludePathFilter(String regex) {
            this.regex = regex;
        }
    
    
        @Override
        public boolean accept(Path path) {
            return !path.toString().matches(regex);
        }
    }
    

    删除数据

    使用 FileSystemdelete() 方法可以永久性删除文件或目录

    public boolean delete(Path f, boolean recursive) throws IOException

    • 如果f是一个文件或空目录, 那么 recursive 的值会被忽略
    • 只有在 recursive 值为 true 时,非空目录及其内容才会被删除, 否则会抛出IOException异常

    1. 在一个表达式中使用通配符来匹配多个文件是比较方便的,无需列举每个文件和目录来指定输入,该操作称为"通配" ↩︎

  • 相关阅读:
    day26:面向对象进阶:set、get、del反射和内置
    day26、面向对象进阶:多态、封装、反射
    day25、 静态属性、类方法、静态方法、组合、继承、
    day24:面向对象设计与面向对象编程、类和对象
    day23:s
    day21、模块
    阿里云ECS服务器挂载磁盘
    Python爬虫总结——常见的报错、问题及解决方案
    Python爬虫实战——反爬机制的解决策略【阿里】
    Python爬虫实战——反爬策略之模拟登录【CSDN】
  • 原文地址:https://www.cnblogs.com/bovenson/p/5730752.html
Copyright © 2011-2022 走看看