python文件hadoop_如何使用python在Hadoop中保存文件?

代码如下:import subprocess

def run_cmd(args_list):

"""

run linux commands

"""

# import subprocess

print('Running system command: {0}'.format(' '.join(args_list)))

proc = subprocess.Popen(args_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

s_output, s_err = proc.communicate()

s_return = proc.returncode

return s_return, s_output, s_err

#Run Hadoop ls command in Python

(ret, out, err)= run_cmd(['hdfs', 'dfs', '-ls', 'hdfs_file_path'])

lines = out.split('\n')

#Run Hadoop get command in Python

(ret, out, err)= run_cmd(['hdfs', 'dfs', '-get', 'hdfs_file_path', 'local_path'])

#Run Hadoop put command in Python

(ret, out, err)= run_cmd(['hdfs', 'dfs', '-put', 'local_file', 'hdfs_file_path'])

#Run Hadoop copyFromLocal command in Python

(ret, out, err)= run_cmd(['hdfs', 'dfs', '-copyFromLocal', 'local_file', 'hdfs_file_path'])

#Run Hadoop copyToLocal command in Python

(ret, out, err)= run_cmd(['hdfs', 'dfs', '-copyToLocal', 'hdfs_file_path', 'local_file'])

hdfs dfs -rm -skipTrash /path/to/file/you/want/to/remove/permanently

#Run Hadoop remove file command in Python

(ret, out, err)= run_cmd(['hdfs', 'dfs', '-rm', 'hdfs_file_path'])

(ret, out, err)= run_cmd(['hdfs', 'dfs', '-rm', '-skipTrash', 'hdfs_file_path'])

#rm -r

#HDFS Command to remove the entire directory and all of its content from #HDFS.

#Usage: hdfs dfs -rm -r

(ret, out, err)= run_cmd(['hdfs', 'dfs', '-rm', '-r', 'hdfs_file_path'])

(ret, out, err)= run_cmd(['hdfs', 'dfs', '-rm', '-r', '-skipTrash', 'hdfs_file_path'])

#Check if a file exist in HDFS

#Usage: hadoop fs -test -[defsz] URI

#Options:

#-d: f the path is a directory, return 0.

#-e: if the path exists, return 0.

#-f: if the path is a file, return 0.

#-s: if the path is not empty, return 0.

#-z: if the file is zero length, return 0.

#Example:

#hadoop fs -test -e filename

hdfs_file_path = '/tmpo'

cmd = ['hdfs', 'dfs', '-test', '-e', hdfs_file_path]

ret, out, err = run_cmd(cmd)

print(ret, out, err)

if ret:

print('file does not exist')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值