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)

如需了解更多,欢迎交流,微信:tan1525859926

你可能感兴趣的:(Linux学习,感悟)