scala - 如何在 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 服务器。您可以为此任务推荐哪种库?
我的推理正确吗?解决我的问题的最佳方法是什么?
解决方案
正如评论中提到的 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 中看到这个问题和示例片段
推荐阅读
- python - 为什么Django中模型方法的返回值没有显示在模板中?
- apache - NiFi 定制处理器在一段时间后不会发布数据到下一个处理器
- c# - 使用 XPath 或 XSLT 从 XML 中排除特定元素
- youtube - YouTube Data API v3 - 类别列表中的 categoryId 问题
- node.js - 得到一个非对象结果,期望来自 create Zapier 的对象?
- reactjs - 如何修复“throw er; //未处理的'错误'事件”?
- sql - 如何使用 SQL 显示具有类别的产品数量
- php - php无法上传csv文件时如何找出错误?
- postgresql - 如何在 PostgreSQL 中缓存重复查询
- javascript - 每次渲染组件时如何将新项目存储在数组中?