hdfs webhdfs 写文件(create file)

Posted 姜小嫌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdfs webhdfs 写文件(create file)相关的知识,希望对你有一定的参考价值。

# _*_ coding=utf-8 _*_

import sys
import os

hosts = {}
cmd1 = ‘‘‘
curl -i -X PUT "http://%s:50070/webhdfs/v1%s?op=CREATE&user.name=op&overwrite=true&replication=3" | grep Location | awk -F ‘ ‘ ‘{print $2}‘
‘‘‘
cmd2 = ‘‘‘
curl -i -X PUT -T %s %s
‘‘‘


def put_file_to_hdfs(hostname, local_file, hdfs_path):
    try:
        # 组装hdfs要写入文件路径
        splits = local_file.split(r"/")
        sub_local_file = splits[len(splits) - 1]
        hdfs_path = hdfs_path + ‘/‘ + sub_local_file
        # 组装url1获得datanode url
        cmd3 = cmd1 % (hostname, hdfs_path)
        process = os.popen(cmd3)
        http_sub = process.read().strip()
        # 组装url2准备写文件
        cmd4 = cmd2 % (local_file, ‘"‘ + http_sub + ‘"‘)
        status = os.system(cmd4)
        if status == 0:
            print ‘put %s successfully.‘ % (local_file)
        else:
            print ‘put %s fail.‘ % (local_file)
    except Exception as e:
        print local_file, e
    finally:
        if process:
            process.close()


if __name__ == ‘__main__‘:
    local_file = sys.argv[1]
    datekey = sys.argv[2]
    hdfs_path = ‘/user/log/‘ + datekey
    webhdfs_host = ‘192.168.254.43‘
    put_file_to_hdfs(webhdfs_host, local_file, hdfs_path)

以上是关于hdfs webhdfs 写文件(create file)的主要内容,如果未能解决你的问题,请参考以下文章

2022-05-13 HDFS 数据存储与数据管理

✨[hadoop3.x系列]HDFS REST HTTP API的使用WebHDFS

[hadoop3.x系列]HDFS REST HTTP API的使用WebHDFS

使用 hadoop 时出现错误“无法从 /webhdfs/v1/?op=LISTSTATUS:服务器错误检索数据”

如何解决hadoop文件无法浏览问题“Failed to retrieve data from /webhdfs/v1/?op=LISTSTATUS: Server Error“

HDFS架构及文件读写流程