副本
zookeeper配置
内部直接修改 直接将其改成自己的就行

外部文件形式 在/etc/clickhouse-server/config.d下创建metrika.xml文件
<?xml version="1.0"?>
<yandex>
<zookeeper-servers>
<node index="1">
<host>spark01</host>
<port>2181</port>
</node>
<node index="2">
<host>spark02</host>
<port>2181</port>
</node>
<node index="3">
<host>spark03</host>
<port>2181</port>
</node>
</zookeeper-servers>
</yandex>
分发给其他机器  在/etc/clickhouse-server/config.xml中添加以下信息:
<zookeeper incl="zookeeper-servers" optional="true" />
<include_from>/etc/clickhouse-server/config.d/metrika.xml</include_from>
 分发集群  到此就配置完成了
测试
副本只能同步数据但是不能同步表结构数据
/clickhouse/table/01/t_order_rep 表示在zookeeper中的路径信息 其中01表示一个分片 rep_102 表示副本名称
分别在三台机子上创建表结构
spark01上创建:
create table t_order_rep2 (
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime
) engine =ReplicatedMergeTree('/clickhouse/table/01/t_order_rep','rep_101')
partition by toYYYYMMDD(create_time)
primary key (id)
order by (id,sku_id);
spark02上创建:
create table t_order_rep2 (
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime
) engine =ReplicatedMergeTree('/clickhouse/table/01/t_order_rep','rep_102')
partition by toYYYYMMDD(create_time)
primary key (id)
order by (id,sku_id);
spark03上创建:
create table t_order_rep2 (
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime
) engine =ReplicatedMergeTree('/clickhouse/table/01/t_order_rep','rep_103')
partition by toYYYYMMDD(create_time)
primary key (id)
order by (id,sku_id);
向spark01中插入数据
insert into t_order_rep2 values
(101,'sku_001',1000.00,'2020-06-01 12:00:00'),
(102,'sku_002',2000.00,'2020-06-01 12:00:00'),
(103,'sku_004',2500.00,'2020-06-01 12:00:00'),
(104,'sku_002',2000.00,'2020-06-01 12:00:00'),
(105,'sku_003',600.00,'2020-06-02 12:00:00');
能在spark02,03上查到 
分片集群
分片就是将一张表的数据分布在不同的节点上,再通过 Distributed 表引擎把数据拼接起来一同使用。 Distributed 表引擎本身不存储数据只是用来管理其他分片
搭建
创建两个分片,第一个分片有一个副本
在config.d下创建metrika-shard.xml文件
<?xml version="1.0"?>
<yandex>
<remote_servers>
<clusters> <!-- 集群名称-->
<shard> <!--集群的第一个分片-->
<internal_replication>true</internal_replication>
<replica> <!--该分片的第一个副本-->
<host>spark01</host>
<port>9000</port>
</replica>
<replica> <!--该分片的第二个副本-->
<host>spark02</host>
<port>9000</port>
</replica>
</shard>
<shard> <!--集群的第二个分片-->
<internal_replication>true</internal_replication>
<replica> <!--该分片的第一个副本-->
<host>spark03</host>
<port>9000</port>
</replica>
</shard>
</clusters>
</remote_servers>
<zookeeper-servers>
<node index="1">
<host>spark01</host>
<port>2181</port>
</node>
<node index="2">
<host>spark02</host>
<port>2181</port>
</node>
<node index="3">
<host>spark03</host>
<port>2181</port>
</node>
</zookeeper-servers>
<macros>
<shard>01</shard> <!--不同机器放的分片数不一样-->
<replica>rep_1_1</replica> <!--不同机器放的副本数不一样-->
</macros>
</yandex>
分发给其他集群  将spark02上的文件进行修改  将spark03上的文件进行修改  在config.xml文件下将文件名进行修改  分发集群  每次配置完config.xml必须重启一次服务
测试
先创建分片表
create table st_order_mt on cluster clusters (
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);
 再创建 Distribute 分布式表
create table test2 on cluster clusters
(
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime
)engine = Distributed(clusters,default, st_order_mt,hiveHash(sku_id));
 向分布式表中插入数据
insert into test2 values
(201,'sku_001',1000.00,'2020-06-03 12:00:00') ;
(202,'sku_002',2000.00,'2020-06-01 12:00:00'),
(203,'sku_004',2500.00,'2020-06-01 12:00:00'),
(204,'sku_002',2000.00,'2020-06-01 12:00:00'),
(205,'sku_003',600.00,'2020-06-02 12:00:00');
第一个分片表  第一个分片表的副本  第二个分片表 
|