【问题标题】:Moving files in Hadoop using the Java API?使用 Java API 在 Hadoop 中移动文件?
【发布时间】:2017-09-22 02:24:39
【问题描述】:

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

我错过了一些基本的东西吗?我能想到的唯一方法是从输入流中读取它并将其写回......然后删除旧副本(糟糕)。

谢谢

【问题讨论】:

    标签: java hadoop hdfs


    【解决方案1】:

    使用FileSystem.rename():

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

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

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

    【讨论】:

    • FileSystem.rename() 是一个抽象方法。那么这将如何运作呢?
    • 您必须获得与正在使用的文件系统(例如 HDFS)相对应的文件系统实现。 FileSystem fs = myPath.getFileSystem(config); fs.rename(myPath, otherPath);
    • 这实际上并没有将源移动到 dst,它只是重新链接它。如果 src 和 dst 是不同的卷,这将返回 false 没有解释或错误:community.mapr.com/thread/7415
    • 嗨@sean,你有解决“错误”问题的方法吗?
    • 我不得不使用'FileUtil.copy' API,就像 Raj R 的以下回答一样
    【解决方案2】:

    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)
    

    【讨论】:

    • 在移动文件的情况下看起来很方便
    【解决方案3】:

    我认为 FileUtilts replaceFile 也可以解决这个目的。 http://hadoop.apache.org/common/docs/current/api/org/apache/hadoop/fs/FileUtil.html#replaceFile(java.io.File, java.io.File)

    【讨论】:

      【解决方案4】:
      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 !!")
              }
      

      【讨论】:

        猜你喜欢
        • 2019-02-17
        • 1970-01-01
        • 2017-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-24
        • 2014-07-26
        相关资源
        最近更新 更多