環境概要:
4台のCentOS7マシンにClickHouseがインストールされている状況を想定する
| ホスト名 | IPアドレス | ソフトウェア | ポート番号 | シャード | レプリカ |
|---|---|---|---|---|---|
| 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 |
1:各ノードへのClickHouseサーバーのインストール手順
略
2:metrika.xml(またはconfig.xml)ファイルの編集
メトリカ設定ファイルを使用しない場合、config.xml内に直接記述することも可能。
設定には以下の3つのセクションが含まれる:remote_servers、zookeeper、macros。すべてのノードでremote_serversとzookeeperは同一だが、macrosのみ個別に設定を行う必要がある。
各ノードのmacros要素において、自身のシャードとレプリカ情報を適切に設定する。以下は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="4">
<host>centf8125</host>
<port>4181</port>
</node>
</zookeeper-servers>
<!-- マクロ設定 -->
<macros>
<shard>01</shard>
<replica>01</replica>
</macros>
</yandex>
3:テーブル作成手順
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テーブルを作成する際、シャードやレプリカ情報は自動的に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())
4:データ挿入方法
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');
5:データ確認手順
レプリカテーブルおよび分散テーブルの仕組みについての詳細は、下記記事を参照のこと: https://blog.csdn.net/qq_36951116/article/details/105511422