环境:
clickhouse:24.8.11.5
1.创建本地表和分布式表
在一个节点上执行即可,我这里配置的分片使用了 default
clickhouse-client --host 192.168.1.102 --port=9000
use db_test
CREATE TABLE tb_test_local ON CLUSTER default
(id Int32,name String,timestamp DateTime DEFAULT now(),day date DEFAULT now()
)
engine = MergeTree
partition by day
order by id
settings index_granularity = 8192;CREATE TABLE tb_test_all ON CLUSTER default(id Int32,name String,timestamp DateTime DEFAULT now(),day date DEFAULT now()
) ENGINE = Distributed(default, db_test, tb_test_local,id);
2.分片规则
分片键sharding_key要求返回一个整形类型的取值,包括Int系列和UInt系列,分片键sharding_key可以使如下几种情况
a.直接指定分片字段,整形字段
ENGINE = Distributed(default, db_test, tb_test_local,id);
B.使用rand()函数
发现不是很均匀
ENGINE = Distributed(default, db_test, tb_test_local,rand());
C.根据整形字段的hash值划分
ENGINE = Distributed(default, db_test, tb_test_local,intHash64(id));
D.hiveHash
ENGINE = Distributed(default, db_test, tb_test_local,hiveHash(id));
3.删除分布上表不会删除本地表
本地表数据也不会删除
4.删除一个分片的本地表,整个分布式表无法使用
5.本地表写入数据分布式表也可以查得到
通过本地表写入
clickhouse-client --host 192.168.1.102 --port=9000
insert into tb_test_local(id,name) values(1,'name1');
insert into tb_test_local(id,name) values(2,'name2');
insert into tb_test_local(id,name) values(3,'name3');
insert into tb_test_local(id,name) values(4,'name4');
insert into tb_test_local(id,name) values(5,'name5');
insert into tb_test_local(id,name) values(6,'name6');
insert into tb_test_local(id,name) values(7,'name7');
insert into tb_test_local(id,name) values(8,'name8');
insert into tb_test_local(id,name) values(9,'name9');
insert into tb_test_local(id,name) values(10,'name10');
6.没有本地表时也可以创建分布式表但是查询错误