python hdfs 模块的一些使用笔记

来源:互联网 发布:爱淘宝 红包 编辑:程序博客网 时间:2024/06/06 04:56

公司新项目用到Hadoop。查了半天资料 python调用hdfs的模块就是hdfs看着顺眼,就用它了。
模块官方文档地址:https://pypi.python.org/pypi/hdfs/2.0.16
https://hdfscli.readthedocs.io/en/latest/api.html#hdfs.client.TokenClient

简单的方法就不写了,很简单直接看官方示例就好了。
让我苦恼了半天的是文件的写入追加,读取EXCEL和CSV文件,并在web界面以表格形式展示。

#追加 append=Truefrom  json import dump,loadwith client.write('r.csv', encoding='utf-8',append=True) as writer:    dump(model,writer)#读取 delimiter='\n' 根据分隔符返回一个生成器with client.read("r.csv",encoding="utf-8",delimiter="\n") as reader:    for line in  reader:        print(line)#读取 chunk_size=1024 根据大小返回一个生成器,流式传输文件with client.read("r.csv",encoding='utf-8',chunk_size=1024) as reader:    for chunk in reader:        print(chunk)