#!coding:utf-8
import sys
from hdfs.client import Client
#设置utf-8模式
reload(sys)
sys.setdefaultencoding( "utf-8" )
#关于python操作hdfs的API可以查看官网:
#https://hdfscli.readthedocs.io/en/latest/api.html
#读取hdfs文件内容,将每行存入数组返回
def read_hdfs_file(client,filename):
#with client.read('samples.csv', encoding='utf-8', delimiter='\n') as reader:
# for line in reader:
#pass
lines = []
with client.read(filename, encoding='utf-8', delimiter='\n') as reader:
for line in reader:
#pass
#print line.strip()
lines.append(line.strip())
return lines
#创建目录
def mkdirs(client,hdfs_path) :
client.makedirs(hdfs_path)
#删除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)
#从
大数据学习(六):Python操作hdfs(包括追加数据文件到hdfs文件)
最新推荐文章于 2024-05-17 06:51:34 发布