使用JavaAPI在Hadoop中移动文件?

使用JavaAPI在Hadoop中移动文件?,java,hadoop,hdfs,Java,Hadoop,Hdfs,我想使用JavaAPI在HDFS中移动文件。我想不出一个办法。FileSystem类似乎只允许在本地文件系统之间移动。。但我想把它们保存在HDFS中,然后把它们移到那里 我缺少一些基本的东西吗?我能想到的唯一方法是从输入流中读取它并将其写回。。。然后删除旧副本(恶心) 谢谢使用: 将路径src重命名为路径dst。可以在本地fs或远程DFS上进行 参数: src-要重命名的路径 dst-重命名后的新路径 返回: true如果重命名成功 抛出: -论失败 我认为FileUtilts replaceF

我想使用JavaAPI在HDFS中移动文件。我想不出一个办法。FileSystem类似乎只允许在本地文件系统之间移动。。但我想把它们保存在HDFS中,然后把它们移到那里

我缺少一些基本的东西吗?我能想到的唯一方法是从输入流中读取它并将其写回。。。然后删除旧副本(恶心)

谢谢

使用:

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

参数:
src
-要重命名的路径
dst
-重命名后的新路径
返回:
true
如果重命名成功
抛出:
-论失败


我认为FileUtilts replaceFile也可以解决这个问题。
,java.io.File)

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)
val fs=FileSystem.get(新配置())
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
FileSystem.rename()
是一种抽象方法。那么这将如何工作呢?您必须获得与正在使用的文件系统(例如HDFS)对应的文件系统实现。
FileSystem fs=myPath.getFileSystem(config);fs.rename(myPath,otherPath);
这实际上并没有将源代码移动到dst,它只是将其重新链接。如果src和dst是不同的卷,这将返回
false
而没有任何解释或错误:Hi@sean,你有没有解决“false”问题的方法?我不得不使用“FileUtil.copy”API,如下面Raj Rinteresting的回答,看起来很方便,以防出现错误f移动文件
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 !!")
        }
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)