依赖库:pip install hdfs
from hdfs.client import Client,InsecureClient
# Insecure 可以解决dr.who权限问题
# hadoop 3.2以后单机访问端口9870 之前50070
# client = InsecureClient('http://172.17.0.2:9870',user='hadoop')
# client = Client('http://172.17.0.2:9870',proxy='hadoop')
# docker 集群
# client = Client('http://172.20.1.0:9870')
#获取指定路径上的具体信息
print(client.list('/'))
# 创建目录
# client.makedirs('lyjtest')
# 冲命名
# client.rename('test','newtest')
# 删除
# client.delete('/lyjtest')
# 上传数据
# client.upload('/','/home/lyj/PycharmProjects/py3/input.txt')
# 下载数据
# client.download('newtest/target.txt','/home/lyj/Desktop')
# 查看根目录
print(client.list('/user/linubu/lyjtest'))
# 查看指定目录的具体信息
# print(client.status('/'))
# 读文件
with client.read('/user/linubu/lyjtest/sss.txt') as reader:
print(reader.read())
dr.who权限问题参考:https://blog.youkuaiyun.com/u010025966/article/details/112977555
python-hdfs API地址:https://hdfscli.readthedocs.io/en/latest/quickstart.html#interactive-shell