gpt4 book ai didi

java - Hadoop 从绝对路径和基本路径获取相对路径

转载 作者:可可西里 更新时间:2023-11-01 14:45:58 30 4
gpt4 key购买 nike

我想在给定绝对基本路径的情况下从绝对路径获取相对路径。是否有任何 Hadoop Java API 可以执行此操作?

例如,如果我的绝对 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 来做相反的事情。

最佳答案

这实际上是在 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 创建一个新路径。

希望对您有所帮助。

关于java - Hadoop 从绝对路径和基本路径获取相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17818356/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com