安装驱动

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

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐