基本信息
clickhouse 基本介绍可以参考:https://clickhouse.com/docs/zh/
python 连接 clickhouse 可以参考:https://clickhouse.com/docs/en/integrations/language-clients/python/intro
clickhouse SQL语法:https://clickhouse.com/docs/en/sql-reference/
clickhouse_connect Github页面:https://github.com/ClickHouse/clickhouse-connect
clickhouse作为一个(据说非常)好用的列式数据库,毛子写的,下面是使用clickhouse_connect
的一些基本操作
基本操作 示例代码
import clickhouse_connect
# 连接数据库
client = clickhouse_connect.get_client(host='127.0.0.1', username="default", password="")
# 创建表
client.command("""CREATE TABLE `test_table` (key UInt16, value String) ENGINE Memory;""")
# 增加数据
data = [
[0, 'value_1'],
[1, 'value_2'],
]
client.insert('test_table', data, column_names=['key', 'value']) # 插入数据 方法1
client.command("""INSERT into test_table ("key","value") values (1,'aaa');""") # 插入数据 方法2
# 查询数据
result_list = client.query('SELECT * FROM test_table where key=1').result_set
print(result_list)
# 修改数据
client.command("""ALTER TABLE test_table UPDATE value='new_value_1' where key=1;""")
# 删除数据
client.command("""ALTER Table test_table DELETE WHERE key=0;""") # 方法1
# SQL 中还可以使用 DELETE FROM test_table WHERE key=0;
# 删除表
client.command("""DROP TABLE IF EXISTS test_table;""")