环境:
python3.6
clickhouse-server-20.8.3.18
1.加载clickhouse驱动包
pip3 install clickhouse_driver
2.表结构
全局表
CREATE TABLE db_test.metric_all
(
`app` String,
`block_qps` Int64,
`count` Int64,
`exception_qps` Int64,
`gmt_create` DateTime DEFAULT now(),
`gmt_modified` DateTime DEFAULT now(),
`id` Int64,
`machine_ip` Nullable(String),
`pass_qps` Int64,
`resource` String,
`resource_code` Int64,
`rt` Float64,
`success_qps` Int64,
`timestamp` DateTime DEFAULT now(),
`day` Date DEFAULT now()
)
ENGINE = Distributed('perftest_3shards_1replicas', 'db_test', 'metric_local', id)
本地表
CREATE TABLE db_test.metric_local
(
`app` String,
`block_qps` Int64,
`count` Int64,
`exception_qps` Int64,
`gmt_create` DateTime DEFAULT now(),
`gmt_modified` DateTime DEFAULT now(),
`id` Int64,
`machine_ip` Nullable(String),
`pass_qps` Int64,
`resource` String,
`resource_code` Int64,
`rt` Float64,
`success_qps` Int64,
`timestamp` DateTime DEFAULT now(),
`day` Date DEFAULT now()
)
ENGINE = MergeTree
PARTITION BY day
ORDER BY id
SETTINGS index_granularity = 8192
3.写入数据
from clickhouse_driver import Client
client = Client(host='192.168.1.118',database='db_test',user='dbaadmin' ,password='123456')
for i in range(201, 100000):
str_i = str(i)
##insert_sql = "insert into user_local(id,name) values ('%s','%s')" % (i, "name" + str_i)
insert_sql = "insert into metric_all(app,block_qps,count,exception_qps,id,machine_ip,pass_qps,resource,resource_code,rt,success_qps) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (
"app" + str_i, i, i, i, i, "machine_ip" + str_i, i, "resource" + str_i, i, i, i)
ans = client.execute(insert_sql)