python 上传本地文件夹到hdfs

背景是这样的,同事有个需求,将自己本地的80多个文件上传到hdfs上,找了hadoop命令没有直接上传文件夹的,所以自己写了个python脚本实现了这个需求,刚开始想着是用shell脚本,但是文件得上传到hdfs集群上,这个比较麻烦,想着本地有python环境,就有了下面的代码。

# -*- coding : utf-8 -*-
import os
from hdfs import InsecureClient
#hdfs 目标路径
base_path = "/hdfs/workspace"
#本地需要上传的路径
dir = "C:/tmp/统计数据"

client_hdfs = InsecureClient('http://192.168.200.81:9870', user='admin')
client_hdfs.makedirs(base_path)


def get_all_dir(path):
    fills_list = os.listdir(path)

    for file_name in fills_list:
        file_abs_path = os.path.join(path, file_name)
        if os.path.isdir(file_abs_path):
            global dir_count
            client_hdfs.makedirs(base_path + "/" + file_name)

            print("dir:", file_name)
            get_all_dir(file_abs_path)
        else:
            mid_path = file_abs_path.replace(dir, "").replace(file_nam
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值