集群:
副本:
目的是为了保障数据的高可靠性
ReplicatedMergeTree中:
第一个参数是分片的zk_path, 一般按照/clickhouse/table/{shard}/{table_name}
的格式写
第二个参数是副本名称, 相同的分片副本名称不能相同
分片:
副本虽然能保证数据的高可靠性, 但是每台Server还是要容纳一个副本里的全部数据
这时就要用到分片, 把一张表里的数据切成多份分给多台Server进行存储, 可以更好地利用有限的服务器资源
-- 创建本地表
create table st_order_mt on cluster gmall_cluster (
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime
) engine =ReplicatedMergeTree('/clickhouse/tables/{shard}/st_order_mt','{replica}')
partition by toYYYYMMDD(create_time)
primary key (id)
order by (id,sku_id);
-- 创建分布式表(要用到本地表, 将其进行分片)
create table st_order_mt_all2 on cluster gmall_cluster
(
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime
)engine = Distributed(gmall_cluster,default, st_order_mt,hiveHash(sku_id));
-- Distributed(集群名称,库名,本地表名,分片键)
配置文件 metrika.xml 需要根据不同情况配置
<macros>
<shard>{xx}</shard>
<replica>{yy}</replica>
</macros>