簡體   English   中英

如何使用python檢查HDFS中是否存在文件

[英]How to check file exists in HDFS using python

import subprocess
def run_cmd(args_list):
    print('Running system command: {0}'.format(' '.join(args_list)))
    proc = subprocess.Popen(args_list, stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
    proc.communicate()
    return proc.returncode

cmd = ['hadoop', 'fs', '-test', '-e', hdfs_file_path]
code = run_cmd(cmd)
if code:
    print 'file not exist'

當我給出此命令以查看HDFS中是否存在文件時,它引發了以下錯誤:

RuntimeError: Error running command: hadoop fs -test -f /app/tmp/1.json. Return code: 1, Error: b''

如何解決這個問題?

我會使用api而不是調用子流程。 為此最好使用一個api,例如Spotify創建的蛇咬 本示例檢查給定文件夾中是否存在文件:

from snakebite.client import Client
client = Client("localhost", 8020, use_trash=False)
return "fileName" in client.ls(['hdfs_path'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM