安装驱动

pip install clickhouse-driver

本地环境

python Python 3.8.8
clickhouse-driver 0.2.8
clickhouse server 20.3

https://github.com/mymarilyn/clickhouse-driver
https://clickhouse-driver.readthedocs.io/en/latest/installation.html

from clickhouse_driver import Client

ck = Client(
	user=clickhouse_config['user'], 
	password=clickhouse_config['password'],
    host=clickhouse_config['host'], port=clickhouse_config['port'], 
    database=clickhouse_config['database']
)

sql = "SELECT id,author,publisher,title,summary FROM bigdata.dw_dim_metadata_book_cip_all WHERE `title` = '互联网' limit 1"
res = ck.execute(sql)  # list

这里特别要注意的一点是端口问题,HTTP协议(默认端口8123);TCP (Native)协议(默认端口号为9000),Python里的clickhouse_driver用的TCP端口9000,DBeaver使用的是HTTP端口。

如果端口错误包报错:UnexpectedPacketFromServerError: Code: 102. Unexpected packet from server 220.202.33.52:19100 (expected Hello or Exception, got Unknown packet)

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐