python操作HDFS

1,python连接hdfs

2,python增删改查hdfs文件

from hdfs.client import Client


#获取HDFS连接
def getHDFSConn():
    client = None
    try:
        client = Client("http://20.58.32.8:50070", root = '/')
    except Exception as e:
        print(e)
    return client



        
#创建目录
def mkdirs(client, hdfs_path):
    client.makedirs(hdfs_path)

    
#上传本地文件到hdfs
def putLocalFileToHDFS(client, hdfs_path, local_path):
    client.upload(hdfs_path, local_path, cleanup=True)
    
    
#数据写入到初次创建文件或者覆盖文件
def writeToHDFS(client, hdfs_path, data):
    client.write(hdfs_path, data, overwrite=True, append=False, encoding='utf-8')

    
#追加数据到hdfs文件
def appendWriteToHDFS(client, hdfs_path, data):
    client.write(hdfs_path, data, overwrite=False, append=True, encoding='utf-8')
    

#DF写入到初次创建文件或者覆盖文件
def writeDFtoHDFS(client, hdfs_path, df):
    client.write(hdfs_path, df.to_csv(index=False, header=False, sep=','), encoding='utf-8', overwrite=True, append=False)

    
#追加DF数据到hdfs文件
def appendWriteDFtoHDFS(client, hdfs_path, df):
    client.write(hdfs_path, df.to_csv(index=False, header=False, sep=','), encoding='utf-8', overwrite=False, append=True)
    
    
#删除hdfs文件
#删除文件夹,该文件夹必须为空
def deleteHDFSfile(client, hdfs_path):
    client.delete(hdfs_path)
    
    
#修改文件夹或者文件名称
def moveOrRename(client, hdfs_src_path, hdfs_dst_path):
    client.rename(hdfs_src_path, hdfs_dst_path)
    
    
#获取文件夹下的文件
def getFileList(client, hdfs_path):
    return client.list(hdfs_path, status=False)


#下载hdfs文件到本地
def getFileFromHDFS(client, local_path, hdfs_path):
    client.download(hdfs_path, local_path, overwrite=False)


#读取文件信息
def readHDFSfile(client, filename):
    lines = []
    with client.read(filename, encoding = 'utf-8', delimiter='\n') as reader:
        for line in reader:
            lines.append(line.strip())
    return lines

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值