hadoop (1.0.4) Path 详解

2023-11-11

Path    对路径进行解析,将参数转换为标准的URI格式,对Path的参数作判断,标准化,字符化等操作。为了便于理解Path,

各位可以先参看URI的详解,链接http://www.cnblogs.com/springside5/archive/2012/05/06/2486245.html



import java.net.*;
import java.io.*;

import org.apache.hadoop.conf.Configuration;

/** Names a file or directory in a {@link FileSystem}.
 * Path strings use slash as the directory separator.  A path string is
 * absolute if it begins with a slash.
 */
public class Path implements Comparable {

  /** The directory separator, a slash. */
  public static final String SEPARATOR = "/";
  public static final char SEPARATOR_CHAR = '/';
  
  public static final String CUR_DIR = ".";
  
  static final boolean WINDOWS
    = System.getProperty("os.name").startsWith("Windows");

 private URI uri;                                // a hierarchical uri      //定义了Path类中的私有变量uri,路径以uri的格式表示,其后对Path的操作也主要通过对uri的操作实现。

  /** Resolve a child path against a parent path. */
  public Path(String parent, String child) {                                // 构造函数,我们可以发现其最终转换为Path(Path,Path)的构造函数。
    this(new Path(parent), new Path(child));
  }

  /** Resolve a child path against a parent path. */
  public Path(Path parent, String child) {
    this(parent, new Path(child));
  }

  /** Resolve a child path against a parent path. */
  public Path(String parent, Path child) {
    this(new Path(parent), child);
  }

  /** Resolve a child path against a parent path. */
  public Path(Path parent, Path child) {                                               //对parent和child做规范化处理
    // Add a slash to parent's path so resolution is compatible with URI's
    URI parentUri = parent.uri;
    String parentPath = parentUri.getPath();
    if (!(parentPath.equals("/") || parentPath.equals("")))                  //重新构造parentUri,在路径的结尾添加一个“/”,让parentUri与URI兼容

      try {
        parentUri = new URI(parentUri.getScheme(), parentUri.getAuthority(),
                      parentUri.getPath()+"/", null, parentUri.getFragment());
      } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
      }
    URI resolved = parentUri.resolve(child.uri);
    initialize(resolved.getScheme(), resolved.getAuthority(),
               normalizePath(resolved.getPath()), resolved.getFragment());       //根据parentUri重新解析child.uri
  }

  private void checkPathArg( String path ) {
    // disallow construction of a Path from an empty string
    if ( path == null ) {
      throw new IllegalArgumentException(
          "Can not create a Path from a null string");
    }
    if( path.length() == 0 ) {
       throw new IllegalArgumentException(
           "Can not create a Path from an empty string");
    }   
  }
  
  /** Construct a path from a String.  Path strings are URIs, but with
   * unescaped elements and some additional normalization. */
  public Path(String pathString) {                                                                                                                  //将String型的路型解析为uri格式
    checkPathArg( pathString );
    
    // We can't use 'new URI(String)' directly, since it assumes things are
    // escaped, which we don't require of Paths. 
    
    // add a slash in front of paths with Windows drive letters
    if (hasWindowsDrive(pathString, false))
      pathString = "/"+pathString;

    // parse uri components
    String scheme = null;
    String authority = null;

    int start = 0;

    // parse uri scheme, if any
    int colon = pathString.indexOf(':');
    int slash = pathString.indexOf('/');
    if ((colon != -1) &&
        ((slash == -1) || (colon < slash))) {     // has a scheme
      scheme = pathString.substring(0, colon);
      start = colon+1;
    }

    // parse uri authority, if any
    if (pathString.startsWith("//", start) &&
        (pathString.length()-start > 2)) {       // has authority
      int nextSlash = pathString.indexOf('/', start+2);
      int authEnd = nextSlash > 0 ? nextSlash : pathString.length();
      authority = pathString.substring(start+2, authEnd);
      start = authEnd;
    }

    // uri path is the rest of the string -- query & fragment not supported
    String path = pathString.substring(start, pathString.length());

    initialize(scheme, authority, path, null);
  }

  /** Construct a Path from components. */
  public Path(String scheme, String authority, String path) {
    checkPathArg( path );
    initialize(scheme, authority, path, null);
  }

  /**
   * Construct a path from a URI
   */
  public Path(URI aUri) {
    uri = aUri;
  }
  
  private void initialize(String scheme, String authority, String path,
      String fragment) {                                                                                                                                       
    try {
      this.uri = new URI(scheme, authority, normalizePath(path), null, fragment)
        .normalize();
    } catch (URISyntaxException e) {
      throw new IllegalArgumentException(e);
    }
  }

  private String normalizePath(String path) {                                                               //标准化输出Path,
    // remove double slashes & backslashes
    if (path.indexOf("//") != -1) {
      path = path.replace("//", "/");
    }
    if (path.indexOf("\\") != -1) {    
      path = path.replace("\\", "/");
    }
    
    // trim trailing slash from non-root path (ignoring windows drive)
    int minLength = hasWindowsDrive(path, true) ? 4 : 1;
    if (path.length() > minLength && path.endsWith("/")) {
      path = path.substring(0, path.length()-1);
    }
    
    return path;
  }

  private boolean hasWindowsDrive(String path, boolean slashed) {
    if (!WINDOWS) return false;
    int start = slashed ? 1 : 0;
    return
      path.length() >= start+2 &&
      (slashed ? path.charAt(0) == '/' : true) &&
      path.charAt(start+1) == ':' &&
      ((path.charAt(start) >= 'A' && path.charAt(start) <= 'Z') ||
       (path.charAt(start) >= 'a' && path.charAt(start) <= 'z'));
  }


  /** Convert this to a URI. */
  public URI toUri() { return uri; }

  /** Return the FileSystem that owns this Path. */
  public FileSystem getFileSystem(Configuration conf) throws IOException {
    return FileSystem.get(this.toUri(), conf);
  }

  /** True if the directory of this path is absolute. */
  public boolean isAbsolute() {
    int start = hasWindowsDrive(uri.getPath(), true) ? 3 : 0;
    return uri.getPath().startsWith(SEPARATOR, start);
  }

  /** Returns the final component of this path.*/
  public String getName() {
    String path = uri.getPath();
    int slash = path.lastIndexOf(SEPARATOR);
    return path.substring(slash+1);
  }

  /** Returns the parent of a path or null if at root. */
  public Path getParent() {
    String path = uri.getPath();
    int lastSlash = path.lastIndexOf('/');
    int start = hasWindowsDrive(path, true) ? 3 : 0;
    if ((path.length() == start) ||               // empty path
        (lastSlash == start && path.length() == start+1)) { // at root
      return null;
    }
    String parent;
    if (lastSlash==-1) {
      parent = CUR_DIR;
    } else {
      int end = hasWindowsDrive(path, true) ? 3 : 0;
      parent = path.substring(0, lastSlash==end?end+1:lastSlash);
    }
    return new Path(uri.getScheme(), uri.getAuthority(), parent);
  }

  /** Adds a suffix to the final name in the path.*/
  public Path suffix(String suffix) {
    return new Path(getParent(), getName()+suffix);
  }

  public String toString() {
    // we can't use uri.toString(), which escapes everything, because we want
    // illegal characters unescaped in the string, for glob processing, etc.
    StringBuffer buffer = new StringBuffer();
    if (uri.getScheme() != null) {
      buffer.append(uri.getScheme());
      buffer.append(":");
    }
    if (uri.getAuthority() != null) {
      buffer.append("//");
      buffer.append(uri.getAuthority());
    }
    if (uri.getPath() != null) {
      String path = uri.getPath();
      if (path.indexOf('/')==0 &&
          hasWindowsDrive(path, true) &&          // has windows drive
          uri.getScheme() == null &&              // but no scheme
          uri.getAuthority() == null)             // or authority
        path = path.substring(1);                 // remove slash before drive
      buffer.append(path);
    }
    if (uri.getFragment() != null) {
      buffer.append("#");
      buffer.append(uri.getFragment());
    }
    return buffer.toString();
  }

  public boolean equals(Object o) {
    if (!(o instanceof Path)) {
      return false;
    }
    Path that = (Path)o;
    return this.uri.equals(that.uri);
  }

  public int hashCode() {
    return uri.hashCode();
  }

  public int compareTo(Object o) {
    Path that = (Path)o;
    return this.uri.compareTo(that.uri);
  }
  
  /** Return the number of elements in this path. */
  public int depth() {
    String path = uri.getPath();
    int depth = 0;
    int slash = path.length()==1 && path.charAt(0)=='/' ? -1 : 0;
    while (slash != -1) {
      depth++;
      slash = path.indexOf(SEPARATOR, slash+1);
    }
    return depth;
  }

  /** Returns a qualified path object. */
  public Path makeQualified(FileSystem fs) {
    Path path = this;
    if (!isAbsolute()) {
      path = new Path(fs.getWorkingDirectory(), this);
    }

    URI pathUri = path.toUri();
    URI fsUri = fs.getUri();
      
    String scheme = pathUri.getScheme();
    String authority = pathUri.getAuthority();
    String fragment = pathUri.getFragment();
    if (scheme != null &&
        (authority != null || fsUri.getAuthority() == null))
      return path;

    if (scheme == null) {
      scheme = fsUri.getScheme();
    }

    if (authority == null) {
      authority = fsUri.getAuthority();
      if (authority == null) {
        authority = "";
      }
    }
    
    URI newUri = null;
    try {
      newUri = new URI(scheme, authority , 
        normalizePath(pathUri.getPath()), null, fragment);
    } catch (URISyntaxException e) {
      throw new IllegalArgumentException(e);
    }
    return new Path(newUri);
  }
  
  /** Returns a qualified path object. */
  public Path makeQualified(URI defaultUri, Path workingDir ) {
    Path path = this;
    if (!isAbsolute()) {
      path = new Path(workingDir, this);
    }

    URI pathUri = path.toUri();
      
    String scheme = pathUri.getScheme();
    String authority = pathUri.getAuthority();
    String fragment = pathUri.getFragment();

    if (scheme != null &&
        (authority != null || defaultUri.getAuthority() == null))
      return path;

    if (scheme == null) {
      scheme = defaultUri.getScheme();
    }

    if (authority == null) {
      authority = defaultUri.getAuthority();
      if (authority == null) {
        authority = "";
      }
    }
    
    URI newUri = null;
    try {
      newUri = new URI(scheme, authority , 
        normalizePath(pathUri.getPath()), null, fragment);
    } catch (URISyntaxException e) {
      throw new IllegalArgumentException(e);
    }
    return new Path(newUri);
  }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

hadoop (1.0.4) Path 详解 的相关文章

  • Java - 如何将特殊字符放入字符串中

    Java 似乎有很好的字符串处理能力 尽管如此 我还是遇到了最简单的问题 我需要动态字符串 它们在运行时更改 因此字符串类型不是一个好的选择 因为它们是不可变的 所以我使用字符数组 设置起来有点痛苦 但至少它们是可以修改的 我想创建一个字符
  • Hashmap并发问题

    我有一个哈希图 出于速度原因 我希望不需要锁定 假设我不介意过时的数据 同时更新它和访问它会导致任何问题吗 我的访问是获取 而不是迭代 删除是更新的一部分 是的 这会导致重大问题 一个例子是向散列映射添加值时可能发生的情况 这可能会导致表重
  • 手动编辑 Jar 以更改包名称

    我有一个来自外部源的 jar 文件 jar 中的所有类都位于 com xyz 包中 我想将所有类移动到 com xyzold 包中 这是否像解压缩 jar 将 xzy 文件夹重命名为 xyzold 并重新压缩它一样简单 或者我还需要修改每个
  • 适用于 Hadoop 的 DynamoDB 输入格式

    我必须使用 Hadoop mapreduce 处理保留在 Amazon Dynamodb 中的一些数据 我在互联网上搜索 Dynamo DB 的 Hadoop InputFormat 但找不到它 我对 Dynamo DB 不熟悉 所以我猜测
  • Google App Engine with Java - 运行 javac.exe 编译器时出错

    在 Windows XP 上 刚刚下载并解压谷歌应用程序引擎java sdk to C Program Files appengine java sdk 我已经安装了jdk C Program Files Java jdk1 6 0 20
  • 如何拦截 REST 端点以接收所有标头?

    我当前的代码是 Path login RequestScoped public class LoginResource GET SecurityChecked public Response getUser HeaderParam AUTH
  • JTable 和 JScrollpane 大小的问题

    我有一个JScrollPane with a JTable在里面 在里面JTable我最初有 3 行 稍后添加行 默认JTable我的 3 行很难看 因为JScrollPane calls getPreferredScrollableVie
  • 有多少种方法可以将位图转换为字符串,反之亦然?

    在我的应用程序中 我想以字符串的形式将位图图像发送到服务器 我想知道有多少种方法可以将位图转换为字符串 现在我使用 Base64 格式进行编码和解码 它需要更多的内存 是否有其他可能性以不同的方式做同样的事情 从而消耗更少的内存 现在我正在
  • 如何在 Eclipse 中使用其他外部 jar 依赖项创建不可运行/不可执行的 jar

    我无法通过 Eclipse 导出向导创建普通的 jar 不可运行 不可执行 它仅创建 jar 文件 但不会导出依赖的 jar 从而在从其他类调用导出的 jar 的方法时出现错误 请帮助 非常感谢 kurellajunior的建议 它是通过使
  • 从 @JsonProperty 值获取枚举常量

    我有一个标有 JsonProperty 的枚举 用于使用 Jackson 进行 JSON 序列化 反序列化 并且希望获取给定字符串 JsonProperty 的枚举值 public enum TimeBucket JsonProperty
  • 异步迭代器

    我有以下代码 while slowIterator hasNext performLengthTask slowIterator next 由于迭代器和任务都很慢 因此将它们放入单独的线程中是有意义的 这是对迭代器包装器的快速而肮脏的尝试
  • Jenkins 的代码覆盖率 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 在 Java 中将弯音发送到 MIDI 音序器

    我了解启动和运行 MIDI 音序器的基础知识 并且希望能够在播放过程中增加 减小序列的音高 但弯音是发送到合成器而不是音序器的消息 我尝试将音序器的接收器设置为合成器的发射器 当我发送弯音短消息时 音序器保持相同的音调 但随后合成器以新的弯
  • 如何找到被点击的JLabel并从中显示ImageIcon?

    这是我的代码 我想知道哪个l单击 然后在新框架中显示该 ImageIcon e getSource 不起作用 final JFrame shirts new JFrame T shirts JPanel panel new JPanel n
  • JSch中如何设置文件类型和文件传输模式?

    我使用 Apache Common NetFTPClient并设置了我的ftpClient在上传文件之前使用如下所示的方法 ftpClient setFileType FTP BINARY FILE TYPE ftpClient setFi
  • 使用 secp256r1 曲线和 SHA256 算法生成 ECDSA 签名 - BouncyCastle

    我正在尝试使用带有 secp256r1 曲线 P256 的 ECDSA 和用于消息哈希的 SHA256 算法生成签名 我也在使用 Bouncy Castle 库 下面的代码 public class MyTest param args pu
  • 失败时石英重试

    假设我有一个这样配置的触发器
  • 摩尔斯电码 至 英语

    我现在的问题是让 摩尔斯电码转英语 正常工作 将英语转换为莫尔斯电码的第一部分工作正常 我知道以前已经有人问过这个问题 但我不知道我做错了什么 我知道我需要在某个地方进行拆分 但我只是不确定将其放在代码中的何处 现在 莫尔斯电码到英语的部分
  • 防止Java实例化的正确方法[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 为什么应该首选 Java 类的接口?

    PMD https pmd github io 将举报以下违规行为 ArrayList list new ArrayList 违规行为是 避免使用 ArrayList 等实现类型 而是使用接口 以下行将纠正违规行为 List list ne

随机推荐

  • Spark性能调优案例

    在实际工作中 性能调优是必不可少的 虽然业务千种百样 实际落地的解决方案可能也不尽相同 但归根结底 调优的最终目的是使得内存 CPU IO均衡而没有瓶颈 基本上 思路都是结合实际业务 数据量从硬件出发 考虑如何充分利用CPU 内存 IO 除
  • Windows下基于IIS服务的SSL服务器的配置

    实验环境 Windows Server 2008 R1 CA Windows Server 2008 R2 web服务器 Windows 7 x64 客户端 3台虚拟机打开桥接模式 保证能够相互ping通 实验原理 CA 根CA 负责为服务
  • 机器学习实战—集成学习

    文章目录 一 简介 1 1 集成学习 1 2 随机森林 二 集成学习 投票分类器 2 1 概念 2 2 代码实现 三 集成学习 bagging和pasting 3 1 简介 3 2 Scikit Learn中使用bagging和pastin
  • MyBatis 通用 Mapper 和 MyBatis-Plus 中的自带 Mapper 方法详解

    目录 概要 MyBatis 通用 Mapper MyBatis Plus 总结 概要 当涉及 MyBatis 的通用 Mapper 和 MyBatis Plus 中的自带 Mapper 方法时 主要关注的是基于继承的方式来实现数据库操作 这
  • 卷积优化

    本文章仅记录卷积优化原理 以下来自转载 转载地址 link http shuokay com 2018 02 21 winograd 首先要明确一点 这里说的卷积是是指 ConvNet 中默认的卷积 而不是数学意义上的卷积 其实 ConvN
  • 与MySQL的零距离接触(三.函数、存储过程、引擎、图形化管理工具)

    金山竹影几千秋 云索高飞水自流 万里长江飘玉带 一轮银月滚金球 远自湖北三千里 近到江南十六州 美景一时观不透 天缘有分画中游 祝大家小年快乐 2018福气生财 一 运算符和函数 准备 连接数据库 mysql uroot proot P33
  • 专才or 通才

    前言 不知道大家有没有这样的感觉 现在的工作专业化程度越来越高 而且是细分方向越来越小 IT领域分到你是计算里面的数据库或者了流式计算引擎 或者是协议存储还是KV存储引擎 专业化的优势 专业化的程度带来了一个好处就是你在这个领域的深入程度越
  • Windows Server 2016虚拟机从零开始安装(超详细)

    目录 一 VMware Workstation虚拟机软件的下载 二 镜像文件的下载 三 Windows Server 2016虚拟机的创建 四 虚拟机系统安装 五 最后 一 VMware Workstation虚拟机软件的下载 官网下载入口
  • SpringData JPA 利用Case实现非规则排序

    开发时候客户需要将巡检单按照状态和开始时间排序 客户需求 但是在数据库中state的值不是正常排序的 审核不通过 5 待处理 0 处理中 1 待审核 3 完成 9 所以总结以下就是需要对象非规则排序 排序是先按照state属性以 5 0 1
  • 基于MATLAB的阔叶树叶片智能识别

    基于MATLAB的阔叶树叶片智能识别 根据叶片的形状 椭圆长轴短轴比 可以初步判断树种 为智能识别打下基础 本文方法是基于MATLAB的图像处理工具和数值计算功能 1 叶片照片的预处理 利用PS去掉背景 2 图像处理 彩色图像转灰度图像
  • 不使用任何框架实现CNN网络

    文章目录 一 问题描述 二 设计简要描述 三 程序清单 四 结果分析 五 调试报告 六 实验小结 一 问题描述 基于Numpy和函数im2col与col2im来实现一个简单的卷积神经网络 将其用于手写体识别 二 设计简要描述 机器学习的三个
  • 硬件入门之什么是mos管

    硬件入门之什么是mos管 文章目录 硬件入门之什么是mos管 一 mos管是什么 MOS管常用于 驱动大功率电路中 MOS选型参数 mos管调参数 二 实际应用场景 1 防反接保护电路 2 防过压保护电路 3 防反接防过压电路一体电路 4
  • Nginx的异步非阻塞

    1 同步与异步 同步与异步的重点在消息通知的方式上 也就是调用结果通知的方式 同步 当一个同步调用发出去后 调用者要一直等待调用结果的通知后 才能进行后续的执行 异步 当一个异步调用发出去后 调用者不能立即得到调用结果的返回 异步调用 要想
  • Druid(Druid.io)简单使用

    Druid简单使用 一 Druid服务进程 Historical进程 Historical进程用于处理历史数据的存储和查询 历史数据包括所以已经被committed的流数据 Historical进程从深度存储 Deep Storage 中下
  • [2020-11-30 ]国产化操作系统调研

    近期 因为公司业务需要 服务器更换为国产操作系统 所以对国产系统的发展进行了一些调研 首先是我国第一款国产操作系统 红旗 2000年成立北京中科红旗软件技术有限公司 2001年的时候 在北京市政府的采购招标中 微软被踢出局 红旗和金山成为了
  • yolo中 LoadImages, LoadScreenshots, LoadStreams

    LoadImages 功能 读取本地图片 视频 摄像头数据 参数说明 path 路径地址 图片文件夹 文件 视频文件夹 文件 或者两者混合都可以 img size resize 或者letterbox转换后的大小 stride letter
  • 基于Matlab的多目标生产调度问题求解方法

    基于Matlab的多目标生产调度问题求解方法 在生产过程中 为了提高生产效率和降低成本 常常需要将多个任务进行合理的安排 而多目标生产调度问题就是要在满足生产需求的前提下 同时最小化生产成本和生产时间等多个目标指标 针对这个问题 我们可以利
  • 鲍春健:从“走进客户”奔向“成为客户”

    站在当下来看 小鹅通的服务力的特殊性在于其可以穿透三层 即以自身的服务力赋能客户的服务力 其中 一层是客户 一层是客户的服务 一层是客户的客户 作者 斗斗 出品 产业家 累计终端用户数达8 2亿 最高日活1400万 累计创造的知识商品数达4
  • 5g网络架构_5G之核心网技术一-核心网网络架构及网元功能介绍

    本文的学习内容 包含前两节 5G总体情况 5G核心网网络架构及网元功能 5G核心网关键技术 5G核心网演进思路 5G网络畅想 采用通用硬件 一张网络满足多样化业务需求 网络平台级运营 灵活适配业务需求 满足能力开放 用户面部署下沉 减小业务
  • hadoop (1.0.4) Path 详解

    Path 对路径进行解析 将参数转换为标准的URI格式 对Path的参数作判断 标准化 字符化等操作 为了便于理解Path 各位可以先参看URI的详解 链接http www cnblogs com springside5 archive 2