3.操作hdfs的api(python)

短命女 2024-02-19 21:04 111阅读 0赞
1.安装依赖包
  1. pip install hdfs
2.连接
  1. # 连接hdfs服务
  2. from hdfs import InsecureClient
  3. client = InsecureClient('http://183.168.10.11:50070', user='root')
3.列出当前目录下的所有文件
  1. print client.list('/')
4.创建新文件,并写入内容
  1. data = '''
  2. this is new file by lys!
  3. '''
  4. with client.write('/foodir/myfile.txt') as writer:
  5. writer.write(data)
5.读取文件
  1. with client.read('/foodir/myfile.txt') as reader:
  2. data = reader.read()
  3. print data
6.文件追加内容
  1. # 通过设置append参数,向一个已经存在的文件追加写入数据
  2. with client.write('/foodir/myfile.txt', append=True) as writer:
  3. writer.write('this is append text by lys! \n')
7.重命名
  1. client.rename('/foodir/myfile.txt', '/foodir/myfile2.txt')
8.下载到指定目录
  1. # 下载到指定目录/home
  2. client.download('/foodir/myfile.txt', '/home/myfile.txt', n_threads=3)
9.创建文件夹
  1. client.makedirs('/testdiretory')
10.上传文件

client.upload(‘目标路径’, ‘本地源路径’)

  1. client.upload('/testdiretory/myfile.txt','/home/myfile.txt' )
11.设置权限
  1. client.set_permission(filepath, 777)

发表评论

表情:
评论列表 (有 0 条评论,111人围观)

还没有评论,来说两句吧...

相关阅读