2013-07-23 1388 views
1

我想从绝对路径获取绝对基路径的相对路径。有没有这样的Hadoop Java API?Hadoop从绝对路径和基本路径获取相对路径

例如,如果我的绝对HDFS路径是abs_path = hdfs://name-node/level1/level2/level3和我的绝对基本路径是abs_base_path = hdfs://name-node/level1,我想从abs_path提取的相对路径,这将是rel_path = level2/level3。我熟悉使用路径构造函数来组合两条路径。

举例来说,如果我有rel_pathabs_base_path,我可以使用重载的构造函数中的一个Path类http://hadoop.apache.org/docs/current/api/org/apache/hadoop/fs/Path打造abs_path,但我无法找到一个API做相反。

+0

我想出的办法是将abs_base_path和abs_path转换为字符串做abs_path_str.replace(abs_base_path_str,StringUtils.EMPTY)。想不到更好的方法来做到这一点。 –

回答

0

如何在使用getParent()递归时构建String,直到当前路径等于基本路径?这是一个帮助功能,可以做你想做的事情。 (我没有测试过,但这个想法可能会帮助)

private static String absolutePathToRelativeString(final Path path, final Path base) { 
    final StringBuilder builder = new StringBuilder(path.toString().length()); 
    Path curPath = new Path(path); 
    while (curPath != null && curPath.depth() != 0 && !curPath.equals(base)) { 
     if (!curPath.equals(path)) { 
      builder.append('/'); 
     } 
     builder.insert(0, curPath.getName()); 
     curPath = curPath.getParent(); 
    } 
    return builder.toString(); 
} 
2

这在FileOutputCommitter的源代码究竟做了。相关功能是

/** 
    * Find the final name of a given output file, given the job output directory 
    * and the work directory. 
    * @param jobOutputDir the job's output directory 
    * @param taskOutput the specific task output file 
    * @param taskOutputPath the job's work directory 
    * @return the final path for the specific output file 
    * @throws IOException 
    */ 
    private Path getFinalPath(Path jobOutputDir, Path taskOutput, 
          Path taskOutputPath) throws IOException { 
    URI taskOutputUri = taskOutput.toUri(); 
    URI relativePath = taskOutputPath.toUri().relativize(taskOutputUri); 
    if (taskOutputUri == relativePath) { 
     throw new IOException("Can not get the relative path: base = " + 
      taskOutputPath + " child = " + taskOutput); 
    } 
    if (relativePath.getPath().length() > 0) { 
     return new Path(jobOutputDir, relativePath.getPath()); 
    } else { 
     return jobOutputDir; 
    } 
    } 

这个想法是为基础目录创建一个URI,然后为这个新的相对化的URI创建一个新的路径。

希望有所帮助。