【python 数据框写入hdfs】windows使用python hdfs模块写入hdfs的一个坑

目标:在windows平台数据框写入hdfs

# -*- encoding=utf-8 -*-

import hdfs
import datetime
import pandas as pd
import time
time1=time.time()


# 自定义获取昨天日期的函数
def getYesterday():
    """
    :return: 获取昨天日期
    """
    today = datetime.date.today()
    oneday=datetime.timedelta(days=1)
    yesterday=today-oneday
    # 日期转字符串
    partition_date=yesterday.strftime('%Y-%m-%d')
    return partition_date



# 数据框写到hdfs文件
def dataframe_write_to_hdfs(client, hdfs_path, dataframe):
    """
    :param client:
    :param hdfs_path:
    :param dataframe:
    :return:
    """
    client.write(hdfs_path, dataframe.to_csv(header=False,index=False,sep="\t"), encoding='utf-8',overwrite=True)


if __name__ == '__main__':

    partition_date=getYesterday()
	# 注意这里用InsecureClient 这种方式,用对应的用户写入对应对应用户拥有的权限的目录下
    client = hdfs.InsecureClient("host:50070",user='admin')
    
    
    hdfs_path = "/user/admin/deploy/user_lable_dimension/partition_type=brush/red-%s.txt" % partition_date

    # 读取数据集
    data=pd.read_csv('C:/Users/xiaohu/Desktop/文本挖掘/用户标签体系二期/用户身份标签/用户下单偏好/刷单/red-%s.txt' % partition_date,sep='\t')
    data.columns = ['app_id', 'user_id', 'login_name', 'cert_no', 'type', 'lable', 'value', 'record_date']

    print(data)

    # 调用函数上传数据集到hdfs
    dataframe_write_to_hdfs(client,hdfs_path,data)
    
    time2 = time.time()
    print(u'ok,上传dataframe数据集到hdfs结束!')
    print(u'总共耗时:' + str(time2 - time1) + 's')

windows平台 写入hdfs 并不会一帆风顺,会出现如下异常信息:

在这里插入图片描述

原因分析:

windows 写入文件到hdfs
本地window 机器需要与 集群的各个DataNode节点保持网络通畅

解决办法:
修改客户机本地的映射,文件是C:\Windows\System32\drivers\etc\host

修改host文件

增加集群的IP地址和域名的对应管理

最后成功写入hdfs

参考链接:https://my.oschina.net/hulingfeng/blog/1617750

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东华果汁哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值