概述
import os
import time
from datetime import datetime
import pandas as pd
# 加载安装包 pandas、clickhouse_driver
from clickhouse_driver import Client
client = Client(host='hadoop102', port='9000', database='alibaba')
for filename in os.listdir('/home/lin/data'):
file = open('/home/lin/data/'+filename, "r")
filelist = file.readlines()
datalist = []
for item in filelist:
num = item.split(",")
pv = 0
cart = 0
buy = 0
fav = 0
if (num[3] == 'pv'):
pv = 1
if (num[3] == 'fav'):
fav = 1
if (num[3] == 'buy'):
buy = 1
if (num[3] == 'cart'):
cart = 1
timeArray = time.localtime(int(num[4]))
date = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
date = pd.to_datetime(date)
#导入
dicts = {'user_id': int(num[0]), 'item_id': int(num[1]), 'cate_id': int(num[2]), 'pv': pv, 'fav': fav,
'cart': cart, 'buy': cart, 'times': date}
datalist.append(dicts)
client.execute("insert into user(user_id,item_id,cate_id,pv,fav,cart,buy,times) values", datalist,
types_check=True)
client.execute("show tables")
#写入的基本单元必须为字典数据类型
client.execute("insert into user(user_id,item_id,cate_id,pv,fav,cart,buy,times) values", datalist, types_check=True)
下面附上python操作ClickHouse的官网教程
Python操作ClickHouse
最后
以上就是玩命荷花为你收集整理的Python将数据写入ClickHouse的全部内容,希望文章能够帮你解决Python将数据写入ClickHouse所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复