繁体   English   中英

如何使用 Python 在 HDFS 中查看传入文件的目录? (Python Script 由 Docker Container 执行;HDFS 中没有 Cronjob)

[英]How to watch a Directory in HDFS for incoming Files using Python? (Python Script is executed by Docker Container; No Cronjob in HDFS)

场景:我的 Python 脚本在部署在 Rancher(kubernetes 集群)中的 docker 容器中运行。 因此容器始终在运行。 我想实现一种方法,该方法正在我的 HDFS 中监视传入文件的目录。 如果有新文件,我希望脚本执行进一步的操作(处理数据的预处理步骤)。 处理完新文件后,应将其删除。 之后脚本也在等待新的传入文件来处理它们。 因此它不应该是 HDFS 中的 cronjob。 我需要由 docker 容器执行的脚本中的代码。 目前我正在使用 hdfs cli 连接到我的 HDFS。 对于 Java,我找到了 INotify,但我需要用 python 来做。

有没有人知道 Python Lib 或其他一些实现这一目标的可能性?

#Schedule below script in crontab for interval of 1 min or 5 min based on your requirement
#Update the parameters(HDFSLocation,FileName,etc) as per the requirement
#Update the script to trigger alert(send mail/trigger another script if newHDFSFileCount > #previousHDFSFileCount)

import subprocess
import os

#Parameters
cwd=os.getcwd()
file='HDFSFileCount.txt'
fileWithPath=cwd+"/"+file
HDFSLocation="/tmp"
previousHDFSFileCount=0
newHDFSFileCount=0
#Calculate New HDFS file count
out = subprocess.Popen(['hadoop','fs','-ls', '/tmp/'], stdout=subprocess.PIPE).communicate()
if out[0][0]==0:
        newHDFSFileCount=0
else:
        newHDFSFileCount=out[0][6]

#
if os.path.exists(fileWithPath):
        f=open(fileWithPath,"r")
        previousHDFSFileCount=f.read()
else:
        f=open(fileWithPath,"w+")
        f.write(newHDFSFileCount)
        previousHDFSFileCount=newHDFSFileCount

f.close()

if (newHDFSFileCount>previousHDFSFileCount):
        f=open(fileWithPath,"w")
        f.write(newHDFSFileCount)
        #print(previousHDFSFileCount)
        #print(newHDFSFileCount)
        f.close()


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM