HDFS 如何从列出的文件中获取相对路径?

os8fio9y  于 2022-12-09  发布在  HDFS
关注(0)|答案(1)|浏览(244)

我写了一个方法,将指定Path的所有文件复制到本地目录。
如何从列出的文件中获取相对路径名?

FileSystem fs = ...;

// remote directory whose files are copied from
org.apache.hadoop.fs.Path path = new Path("/path/to/the/directory");

// local directory into which files are copied.
java.nio.file.Path directory = ...;

for (LocatedFileStatus status : fs.listFiles(path, true)) {

    // status: HdfsNamedFileStatus{path=hdfs://.../path/to/the/directory/.../...; ...}
    // status.getPath(): hdfs://.../path/to/the/directory/.../...

    // How can I get a relative path name for each status
    // against the source path
    // so that I can createDirectories on the target directory?

    // I need the .../... part from hdfs://.../path/to/the/directory/.../...
    // so that I can call Files.createDirectories(directory.resolve(.../...))
}
uidvcgyl

uidvcgyl1#

我认为您可以用途:

public FileStatus[] listStatus(Path p)

获取目录的内容。

相关问题