ClickHouse Replication

2019-09-29  本文已影响0人  白奕新

0、一个重要参数:internal_replication

internal_replication:是否只写入所有replica中的一台。

1、依赖ZooKeeper进行复制

SQL
CREATE TABLE table_name
(
    EventDate DateTime,
    CounterID UInt32,
    UserID UInt32
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{layer}-{shard}/table_name', '{replica}')
PARTITION BY EventDate
ORDER BY (CounterID, EventDate, intHash32(UserID)
SETTINGS index_granularity = 8192;
item

(1)/clickhouse/tables/{layer}-{shard}/table_name:代表的是这张表在zk上的路径,如果要配置复制则需要配置相同的路径。即配置在相同shard里面的不同replica的机器需要配置相同的路径,不同shard的路径不同。
(2){replica}:分片的名称,可以理解是机器名,即需要每台机器都不同
(3)集群的配置,{layer}{shard}{replica}配置在配置文件的中

<?xml version="1.0"?>
<yandex>
    <remote_servers>
        <common>
            <shard>
                <weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>CH-IP1</host>
                    <port>53090</port>
                </replica>
                <replica>
                    <host>CH-IP2</host>
                    <port>53090</port>
                </replica>
            </shard>
            <shard>
                <weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>CH-IP3</host>
                    <port>53090</port>
                </replica>
                <replica>
                    <host>CH-IP4</host>
                    <port>53090</port>
                </replica>
            </shard>
        </common>
    </remote_servers>


    <zookeeper>
        <node index="1">
            <host>IP1</host>
            <port>50000</port>
        </node>
        <node index="2">
            <host>IP2</host>
            <port>50000</port>
        </node>
        <node index="3">
            <host>IP3</host>
            <port>50000</port>
        </node>
    </zookeeper>

    <macros replace="replace">
        <shard>p1</shard>
        <replica>CH-IP1</replica>
    </macros>
</yandex>

(4)ZooKeeper version:3.4.5 or later
(5)internal_replication配置为true
(6)使用ZooKeeper以及复制不会影响SELECT的性能
(7)数据会被发送到写入的那台服务器,再异步同步到其他Replica上。如果replica在当时不可用,会等到可用的时候再写入。

2、写分布式表进行复制

区别于使用ZooKeeper进行复制,写分布式表进行复制类似于ClickHouse帮你进行多张复制表的多写
(1)local表只需要配置为MergeTree即可,不需要配置为复制表
(2)配置<internal_replication>false</internal_replication>
(3)写分布式表

3、两种复制方式的比较

image.png

使用写分布式表的缺点:
(1)使用写分布式表进行复制,则可能出现多写一边成功一边失败的情况,数据的一致性不可控
(2)在一台服务器宕机一阵子以后,再恢复过来则这个时间段里面的数据不能自动恢复从另外的replica恢复过来。

4、其他

上一篇 下一篇

猜你喜欢

热点阅读