使用Java API在Hadoop中移动文件?

我想使用Java API在HDFS中移动文件。 我无法想办法做到这一点。 FileSystem类似乎只允许允许移入和移出本地文件系统..但我想将它们保存在HDFS中并将它们移动到那里。

我错过了什么基本的东西? 我可以想象的唯一方法是从输入流中读取它并将其写回…然后删除旧副本(yuck)。

谢谢

使用FileSystem.rename() :

public abstract boolean rename(Path src, Path dst) throws IOException 

将Path src重命名为Path dst 。 可以在本地fs或远程DFS上进行。

参数:
src – 要重命名的路径
dst – 重命名后的新路径
返回:
如果重命名成功,则为true
抛出:
IOException – 失败时

java.nio。*方法可能无法在HDFS上运行。 所以找到了以下解决方案。

使用org.apache.hadoop.fs.FileUtil.copy API将文件从一个目录移动到另一个目录

 val fs = FileSystem.get(new Configuration()) val conf = new org.apache.hadoop.conf.Configuration() val srcFs = FileSystem.get(new org.apache.hadoop.conf.Configuration()) val dstFs = FileSystem.get(new org.apache.hadoop.conf.Configuration()) val dstPath = new org.apache.hadoop.fs.Path(DEST_FILE_DIR) for (file <- fileList) { // The 5th parameter indicates whether source should be deleted or not FileUtil.copy(srcFs, file, dstFs, dstPath, true, conf) 
 hdfsDirectory="hdfs://srcPath" val conf = new org.apache.hadoop.conf.Configuration() val src:Path = new org.apache.hadoop.fs.Path(hdfsDirectory) val fs = FileSystem.get(src.toUri,conf) val srcPath: Path = new Path("hdfs://srcPath") val srcFs =FileSystem.get(srcPath.toUri,conf) val dstPath:Path =new Path("hdfs://targetPath/") val dstFs =FileSystem.get(dstPath.toUri,conf) val exists = fs.exists(new org.apache.hadoop.fs.Path(hdfsDirectory)) val status:Array[FileStatus] = fs.listStatus(new Path(hdfsDirectory)) if (status.length>0) { status.foreach(x => { println("My files: " + x.getPath) FileUtil.copy(srcFs, x.getPath, dstFs, dstPath, true, conf) println("Files moved !!" +x.getPath) } )} else{ println("No Files Found !!") }