首页 > 解决方案 > 如何在 Scala/Spark 中将文件从 Hadoop (hdfs) 复制到远程 SFTP 服务器?

问题描述

Hadoop我有Excel文件的文件系统中。

我的任务是将该文件复制Hadoop到我的应用程序中的远程SFTP服务器Scala/Spark

我已经形成了直接它不起作用的观点。如果我的担心是正确的,我需要采取以下步骤:

1) 将 Hadoop 中的 excel 文件删除到本地目录。例如,我可以使用 Scala DSL:

import scala.sys.process._
s"hdfs dfs -copyToLocal /hadoop_path/file_name.xlsx /local_path/" !

2) 从本地目录发送文件到远程 SFTP 服务器。您可以为此任务推荐哪种库?

我的推理正确吗?解决我的问题的最佳方法是什么?

标签: scalaapache-sparkhadoop

解决方案


正如评论中提到的 spark-sftp 是不错的选择

如果不是,您可以尝试以下来自apache-commons-ftp库的示例代码。它将列出所有远程文件。同样,您也可以删除文件。未经测试的请尝试一下。

选项1:

import java.io.IOException

import org.apache.commons.net.ftp.FTPClient

//remove if not needed
import scala.collection.JavaConversions._

object MyFTPClass {

  def main(args: Array[String]): Unit = {
// Create an instance of FTPClient
    val ftp: FTPClient = new FTPClient()
    try {
// Establish a connection with the FTP URL
      ftp.connect("ftp.test.com")
// Enter user details : user name and password
      val isSuccess: Boolean = ftp.login("user", "password")
      if (isSuccess) {
// empty array is returned
        val filesFTP: Array[String] = ftp.listNames()
        var count: Int = 1
// Iterate on the returned list to obtain name of each file
        for (file <- filesFTP) {
          println("File " + count + " :" + file) { count += 1; count - 1 }
        }
      }
// Fetch the list of names of the files. In case of no files an
// Fetch the list of names of the files. In case of no files an
      ftp.logout()
    } catch {
      case e: IOException => e.printStackTrace()

    } finally try ftp.disconnect()
    catch {
      case e: IOException => e.printStackTrace()

    }
  }

}

选项 2: 有一个叫做jsch 库的东西,你可以从 SO 中看到这个问题和示例片段


推荐阅读