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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013421629/article/details/82805614

目标:在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

猜你喜欢

转载自blog.csdn.net/u013421629/article/details/82805614