本文介绍如何在 Python 开发环境连接并访问 ByteHouse 企业版。
ByteHouse 兼容下列开源 ClickHouse Python Driver 程序:
说明
需要 Python 3.7 或更高版本的支持。
ClickHouse Python驱动程序通过 TCP 接口 访问 ByteHouse 企业版。
访问 ByteHouse 所需的连接信息,请参考获取集群连接信息。
可以通过如下命令,获取最新发布版本的 clickhouse-driver。
pip install clickhouse-driver
开发版本通过如下命令安装。
pip install git+https://github.com/mymarilyn/clickhouse-driver@master#egg=clickhouse-driver
根据您的 Python 版本,您可能需要安装以下依赖项:
本章节介绍通过 mymarilyn/clickhouse-driver 程序连接 ByteHouse 的基本用法,您可以在程序 Github 主页 获取最新的文档和发布版本信息。
可参考下面代码样例设置 ByteHouse 连接信息,具体内容可以根据"前提条件"中获取的信息填写。
# 替换下面的 {HOST}、{PORT}、{username}、{password} 占位符 # 驱动程序通过 TCP 接口访问,PORT 默认值为9000 from clickhouse_driver import Client from clickhouse_driver import connect client = Client(host='{HOST}',port ={PORT}, user='{username}', password='{password}') conn = connect(host='{HOST}',port ={PORT}, user='{username}', password='{password}') cursor = conn.cursor()
# 替换下面的 {HOST}、{PORT}、{username}、{password} 占位符 # 驱动程序通过 TCP 接口访问,PORT 默认值为9000 from clickhouse_driver import Client from clickhouse_driver import connect client = Client(host='{HOST}',port ={PORT}, user='{username}', password='{password}')#################################### #################################### # from clickhouse_driver import Client # client #################################### ### 查询示例 query_result = client.execute("select query_id, type, event_date, query_start_time,query_kind,databases,ProfileEvents,thread_ids from system.query_log where event_date = '2022-10-17' and query_id = '4b51b210-037f-4fa0-bb4f-42ebcfa090f5'") print (query_result) ### DDL #client.execute('CREATE TABLE new_tables (key UInt32, value String, metric Float64) ENGINE MergeTree ORDER BY key') #client.execute('ALTER TABLE new_tables add column test String') #client.execute('ALTER TABLE new_tables modify column test Int64') #client.execute('ALTER TABLE new_tables drop column test') #client.execute('DROP TABLE new_tables') ### 插入 insert data = [[100, 'value1', 1.1], [200, 'value2',2.2]] #client.execute('INSERT INTO new_tables (*) VALUES', data) #client.execute('INSERT INTO new_tables (key) SELECT * FROM system.numbers LIMIT %(limit)s',{'limit': 3}) #client.execute('INSERT INTO new_tables (key) VALUES',[{'key': 100}]) #client.execute('INSERT INTO table_from_file FROM INFILE 'input.csv' FORMAT CSV;') #################################### # from clickhouse_driver import connect # cursor #################################### conn = connect(host='10.145.132.136', port=9000,user='dba', password='xx') cursor = conn.cursor() ### 查询query cursor.execute("select * from system.clusters") #cursor.execute('SHOW TABLES') #print(cursor.fetchall()) res = cursor.fetchone() while res: print(res) res = cursor.fetchone() ### DDL: cursor.execute('DROP TABLE IF EXISTS test') print(cursor.fetchall()) #cursor.execute('CREATE TABLE test (x Int32) ENGINE = Memory') #print(cursor.fetchall()) ### 插入 INSERT: cursor.executemany('INSERT INTO test (x) VALUES',[{'x': 100}]) print(cursor.rowcount) #cursor.executemany('INSERT INTO test (x) VALUES', [[200]]) #print(cursor.rowcount) #cursor.execute('INSERT INTO test (x) SELECT * FROM system.numbers LIMIT %(limit)s',{'limit': 3}) #print(cursor.rowcount)