设为首页 加入收藏

TOP

python读取hdfs数据
2019-04-07 12:16:29 】 浏览:144
Tags:python 读取 hdfs 数据

加载包

from hdfs.client import Client

class Process_Data_Hdfs():

    def __init__(self):
        self.client = Client("http://hadoop1:50070")
        self.filename = "/user/hdfs/read.txt"
读取hdfs文件内容,将每行存入数组返回
 def read_hdfs_file(self):
        # with client.read('samples.csv', encoding='utf-8', delimiter='\n') as reader:
        #  for line in reader:
        # pass

        lines = []
        with self.client.read(self.filename, encoding='utf-8', delimiter='\n') as reader:
            for line in reader:
            # pass
            # print line.strip()
            lines.append(line.strip())
    return lines
# 创建目录
def mkdirs(self, hdfs_path):
    self.client.makedirs(hdfs_path)
删除hdfs文件
def delete_hdfs_file(self,hdfs_path):
    self.client.delete(hdfs_path)
上传文件到hdfs
def put_to_hdfs(self, local_path, hdfs_path):
    self.client.upload(hdfs_path, local_path, cleanup=True)
从hdfs获取文件到本地
def get_from_hdfs(self, hdfs_path, local_path):
    self.client.download(hdfs_path, local_path, overwrite=False)
追加数据到hdfs文件
def append_to_hdfs(self, hdfs_path, data):
    self.client.write(hdfs_path, data, overwrite=False, append=True)
覆盖数据写到hdfs文件
def write_to_hdfs(self, hdfs_path, data):
    self.client.write(hdfs_path, data, overwrite=True, append=False)
# 移动或者修改文件
def move_or_rename(self,hdfs_src_path, hdfs_dst_path):
    self.client.rename(hdfs_src_path, hdfs_dst_path)
# 返回目录下的文件
def list(self,hdfs_path):
    return self.client.list(hdfs_path, status=False)
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇scala删除hdfs中已有的路径 下一篇HDFS修改副本位置

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目