HDFS:使用Java / Scala API移动多个文件

我需要使用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中移动一组文件?

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

但是如果你想要*.xml那么有像globfilter这样的filter文件。

 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) { //  //  }