python使用pyhdfs上传文件到hdfs

本文属于转载,仅用于做个笔记。

# -*- coding:utf-8 -*-

import pyhdfs

'''pip install pyhdfs'''
class FileManager(object):

    # upload file to hdfs from local file system
    def file_upload(self, host, user_name, local_path, hdfs_path):
        print("file upload start")
        fs = pyhdfs.HdfsClient(hosts=host, user_name=user_name)
        print(fs.listdir('/'))
        fs.copy_from_local(local_path, hdfs_path)
        print("file upload finish")

    # download file from hdfs file system
    def file_down(self, host, user_name, local_path, hdfs_path):
        print("file download start")
        fs = pyhdfs.HdfsClient(hosts=host, user_name=user_name)
        fs.copy_to_local(hdfs_path, local_path)
        print("file download finish")


if __name__ == '__main__':
    file_manager = FileManager()
    file_manager.file_upload("192.168.12.101:50070", "admin", u"airport_inf.xls", u"/airport_fare_data/his_data/airport_inf.xls")
    # file_manager.file_down("ip:50070", "admin", u"local_file.xlsx", u"hdfs_file.xlsx")

 上传结果图:

python使用pyhdfs上传文件到hdfs_第1张图片

你可能感兴趣的:(Python)