栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

python操作hdfs

python操作hdfs

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
安装依赖包
    pip install hdfs
    pip install thrift
"""

from hdfs.client import Client


# 删除hdfs文件
def delete_hdfs_file(client, hdfs_path):
    client.delete(hdfs_path)


# 上传文件到hdfs
def put_to_hdfs(client, local_path, hdfs_path):
    client.upload(hdfs_path, local_path, cleanup=True)


def read_hdfs_file(client, hdfs_path):
    # 读文件内容
    with client.read(hdfs_path) as reader:
        # 解码(源数据为`b''`样式)
        out = reader.read().decode("utf-8")
        #    测试输出
        print(out)


def main():
    client = Client("http://master:9870", root='root')
    # delete_hdfs_file(client, '/tmp/hdfs_hbase_operation.py')
    # put_to_hdfs(client, "D:workspacepython_workspacestudent_dianpingcomexamplehdfs_hbase_operation.py", '/tmp')
    read_hdfs_file(client, '/tmp/c45.log')


if __name__ == '__main__':
    main()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/758628.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号