【问题标题】:HDFS: move multiple files using Java / Scala APIHDFS:使用 Java / Scala API 移动多个文件
【发布时间】:2015-12-04 20:34:58
【问题描述】:

我需要使用 Java / Scala 程序在 HDFS 中移动与给定正则表达式相对应的多个文件。例如,我必须将名称为 *.xml 的所有文件从文件夹 a 移动到文件夹 b

使用 shell 命令我可以使用以下命令:

bin/hdfs dfs -mv a/*.xml b/

我可以使用 Java API 移动单个文件,使用以下代码(Scala 语言),使用 FileSystem 类上的 rename 方法:

// Prepare initial configuration
val conf = new Configuration()
conf.set("fs.defaultFS", "hdfs://hdfs:9000/user/root")
val fs = FileSystem.get(conf)
// Move a single file
val ok = fs.rename(new Path("a/file.xml"), new Path("b/file.xml"));

据我所知,Path 类代表一个 URI。那么,我不能通过以下方式使用:

val ok = fs.rename(new Path("a/*.xml"), new Path("b/"));

有没有办法通过 Java / Scala API 在 HDFS 中移动一组文件?

【问题讨论】:

    标签: java scala hadoop hdfs


    【解决方案1】:

    你可以使用fs.rename(new Path("a"), new Path("b"))

    但是如果你想拥有*.xml 有像 globfilter 这样的过滤器文件。

    FileSystem fs = FileSystem.get(URI.create(arg0[0]), conf);
    Path path = new Path(arg0[0] + arg0[1]); // arg0[1] NYSE_201[2-3]
    //arg0[0] is base path
    //ar0[1] uses regular expression
    
    FileStatus[] status = fs.globStatus(path);
    Path[] paths = FileUtil.stat2Paths(status);
    for (Path p : paths) {
        // <loops all the source paths>
        // <need to implement logic to rename the paths using fs.rename>
    }
    

    【讨论】:

    • 感谢您的回答。实现我需要的另一种方法是使用方法listStatus 接受PathPathFilter,它允许您过滤列表的结果。
    • 在哪里可以找到我们可以使用 fs 执行的命令列表?任何指针,例如 fs.rename fs.globStatus 等等。提前致谢
    • 我在看正确的链接link 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    相关资源
    最近更新 更多