背景介绍:
有四台CentOS7服务器安装了ClickHouse
| HostName | IP | 安装程序 | 程序端口 | shard(分片) | replica(备份) |
| centf8118.sharding1.db | 192.168.81.18 | clickhouse-server,clickhouse-client | 9000 | 01 | 01 |
| centf8119.sharding2.db | 192.168.81.19 | clickhouse-server,clickhouse-client | 9000 | 01 | 02 |
| centf8120.sharding3.db | 192.168.81.20 | clickhouse-server,clickhouse-client | 9000 | 02 | 01 |
| centf8125 | 192.168.81.25 | clickhouse-server,clickhouse-client | 9000 | 02 | 02 |
一:在所有节点安装clickhouse-server。
略
二:修改metrika.xml(或config.xml)文件。
如果没有用到包含的metrika.xml配置文件,也可以直接配置在config.xml文件。
涉及三部分remote_servers,zookeeper,macros,所有的节点的remote_servers,zookeeper是一样的,不同的是macros。
每个节点根据自己的角色修改shard和replica的值;下面给出sharding1这个节点的配置。
<yandex>
<!-- 集群配置 -->
<clickhouse_remote_servers>
<!-- 2分片2备份 -->
<cluster_2shards_2replicas>
<!-- 数据分片1 -->
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>centf8118.sharding1.db</host>
<port>9000</port>
</replica>
<replica>
<host>centf8119.sharding2.db</host>
<port>9000</port>
</replica>
</shard>
<!-- 数据分片2 -->
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>centf8120.sharding3.db</host>
<port> 9000</port>
</replica>
<replica>
<host>centf8125</host>
<port> 9000</port>
</replica>
</shard>
</cluster_2shards_2replicas>
</clickhouse_remote_servers>
<!-- zookeeper 配置 -->
<zookeeper-servers>
<node index="1">
<host>centf8118.sharding1.db</host>
<port>4181</port>
</node>
<node index="2">
<host>centf8119.sharding2.db</host>
<port>4181</port>
</node>
<node index="3">
<host>centf8120.sharding3.db</host>
<port>4181</port>
</node>
<node index="3">
<host>centf8125</host>
<port>4181</port>
</node>
</zookeeper-servers>
<!-- macros配置 -->
<macros>
<shard>01</shard>
<replica>01</replica>
</macros>
</yandex>
三:建表
3.1:创建实体表:t_s2_r2
CREATE TABLE t_s2_r2 ( dt Date, path String ) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/t_s2_r2','{replica}',dt, dt, 8192) ;
在每个节点创建t_s2_r2,不需要手动替换shard和replica,建表的时候会根据shard和replica数据自行在zookeeper中注册。
3.2:创建分布表:t_s2_r2_all
这个表在任何一个节点创建都行,t_s2_r2_all就像一个视图,指向所有的分片,数据真正的存储在每个节点的t_s2_r2表。
# 创建分区表
CREATE TABLE t_s2_r2_all AS t_s2_r2 ENGINE = Distributed(cluster_2shards_2replicas, datasets, t_s2_r2, rand())
四:插入数据
insert into t_s2_r2_all values('2020-09-01','path1'); insert into t_s2_r2_all values('2020-09-02','path2'); insert into t_s2_r2_all values('2020-09-03','path3'); insert into t_s2_r2_all values('2020-09-04','path4');
五:查看数据
复制表和分布表原理: https://blog.csdn.net/qq_36951116/article/details/105511422