【问题标题】:How to copy files incrementally from FTP server to Hadoop HDFS如何将文件从 FTP 服务器增量复制到 Hadoop HDFS
【发布时间】:2024-01-14 06:57:01
【问题描述】:

我们有一个 FTP 服务器,每天都有很多文件上传到 FTP 服务器,我需要将所有这些文件复制到 HDFS 中。

每次它应该只下载增量文件,即如果它第一次下载了 10 个文件,然后 FTP 上传了 5 个新文件;在下一次工作迭代中,它应该只在 HDFS 中下载新的 5 个文件。

我们没有使用 Nifi 或 Kafka 连接。

我们有什么好的解决方案来完成这项任务吗?

【问题讨论】:

  • 你在你的环境中使用 shell 脚本吗?
  • @roh 不,但是如果使用 shell 是优雅的解决方案,请讨论
  • @nilesh1212..您愿意分享您实施的解决方案的详细信息吗..我也遇到同样的问题..由于空间问题,我不想将我的文件 ftp 到本地

标签: hadoop ftp hdfs


【解决方案1】:

您可以使用 LFTP 作业中的触摸文件来实现此目的,下面是我的解释和代码。检查每一步的 cmets。

#!bin/bash
#SomeConfigs
TOUCHFILE='/somepath/inYourLocal/someFilename.touch'
RemoteSFTPserverPath='/Remote/Server/path/toTheFiles'
LocalPath='/Local/Path/toReceiveTheFiles'
FTP_Server_UserName='someUser'
FTP_Server_Password='SomePassword'
ServerIP='//127.12.11.35'

#Transfer files from FTP Server #This is the main command
ftp_command="lftp -e 'mirror --only-missing --newer-than=${TOUCHFILE} --older-than=now-2minutes --parallel=4 --no-recursion --include "SomeFileName*.csv"  ${RemoteSFTPserverPath}/ ${LocalPath}/;exit' -u ${FTP_Server_UserName},${FTP_Server_Password} sftp:${ServerIP}"

#CommandToexecute The Job
eval ${ftp_command}

#After finishing the lftp job You have to update the touch file for the next job
#This will update to current timestamp
touch /somepath/inYourLocal/someFilename.touch

#If you want to update with the last file received time
TchDate=$(stat -c %y "$(ls -1t ${LocalPath} | head -n1)" | date)
touch -d ${TchDate} /somepath/inYourLocal/someFilename.touch

#Stat on latest file in remote server #You can do this way also
TchDate=$(ssh -o StrictHostKeyChecking=no ${FTP_Server_UserName}@${FTP_Server_Password} "stat -c %y \"$(ls -1t ${RemoteSFTPserverPath}/ | head -n1)\" | date")
touch -d ${TchDate} /somepath/inYourLocal/someFilename.touch

#Once you have the files in your local you can copy them to hdfs

hdfs dfs -put -f /Local/Path/toReceiveTheFiles/*.csv /HDFS/PATH

#Remove the files in local so that you can accommodate for the upcoming files
rm -r -f /Local/Path/toReceiveTheFiles/*.csv

在 LFTP 工作中,您有很多选择 man lftp 将是您的最佳来源

【讨论】:

  • @nilesh1212 如果您卡在某个地方,请告诉我,因为我快速编写了上述代码,可能会有一些拼写错误,请注意。
最近更新 更多