clickhouse之python操作
2021/9/3 22:08:54
本文主要是介绍clickhouse之python操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
使用python来对clickhouse进行操作
1.clickhouse-driver (mymarilyn/clickhouse-driver: ClickHouse Python Driver with native interface support (github.com))
纯客户端:
>>> from clickhouse_driver import Client >>> >>> client = Client('localhost') >>> >>> client.execute('SHOW TABLES') [('test',)] >>> client.execute('DROP TABLE IF EXISTS test') [] >>> client.execute('CREATE TABLE test (x Int32) ENGINE = Memory') [] >>> client.execute( ... 'INSERT INTO test (x) VALUES', ... [{'x': 100}] ... ) 1 >>> client.execute('INSERT INTO test (x) VALUES', [[200]]) 1 >>> client.execute( ... 'INSERT INTO test (x) ' ... 'SELECT * FROM system.numbers LIMIT %(limit)s', ... {'limit': 3} ... ) [] >>> client.execute('SELECT sum(x) FROM test') [(303,)]
使用数据库接口:
>>> from clickhouse_driver import connect >>> >>> conn = connect('clickhouse://localhost') >>> cursor = conn.cursor() >>> >>> cursor.execute('SHOW TABLES') >>> cursor.fetchall() [('test',)] >>> cursor.execute('DROP TABLE IF EXISTS test') >>> cursor.fetchall() [] >>> cursor.execute('CREATE TABLE test (x Int32) ENGINE = Memory') >>> cursor.fetchall() [] >>> cursor.executemany( ... 'INSERT INTO test (x) VALUES', ... [{'x': 100}] ... ) >>> cursor.rowcount 1 >>> cursor.executemany('INSERT INTO test (x) VALUES', [[200]]) >>> cursor.rowcount 1 >>> cursor.execute( ... 'INSERT INTO test (x) ' ... 'SELECT * FROM system.numbers LIMIT %(limit)s', ... {'limit': 3} ... ) >>> cursor.rowcount 0 >>> cursor.execute('SELECT sum(x) FROM test') >>> cursor.fetchall() [(303,)]
链接的使用使用的参数:
host: 地址
port: 端口
user: 用户名
password: 密码
database: 数据库名称
send_receive_timeout: 超时时间
这篇关于clickhouse之python操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南