python3.5.2连接hdfs

因最近有人请教python连接hdfs方法,简单整理如下:

#!/usr/bin/env python
#-*- coding: utf-8 -*-
from hdfs.client import Client

class Process_Data_Hdfs():

    def __init__(self):
        self.client = Client("http://10.16.14.101:50070")
        self.filename="/hdp/daytime.csv"

    # 读取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)

if __name__ == '__main__':
    data = Process_Data_Hdfs().read_hdfs_file()
    print(data)

猜你喜欢

转载自blog.csdn.net/tandelin/article/details/88249471