首页 > 技术文章 > 手把手教你:将 ClickHouse 集群迁至云上

TXcloudbigdata 2020-09-29 18:08 原文

前言

 

随着云上 ClickHouse 服务完善,越来越多的用户将自建 ClickHouse 服务迁移至云上。对于不同数据规模,我们选择不同的方案:

  • 对于数据量比较小的表,通常小于10GB 情况下,可以将数据导出为 CSV 格式,在云上集群重新写入数据;

  • 使用 clickhouse 发行版自带工具 clickhouse-copier 来完成。

 

本文详解 clickhouse-copier 完成跨 ClickHouse 集群数据迁移(当然也适用于用户集群内部数据不同表间数据迁移)。

 

一、Zookeeper 集群准备

 

如果已经有 Zookeeper 集群,请忽略本章节。

由于 clickhouse-copier 需要 Zookeeper 存储数据迁移任务信息,需要部署一个 Zookeeper 集群。

Zookeeper 集群到源 ClickHouse 集群与目标 ClickHouse 集群之间的网络是正常的。

在本文中,我们部署一个单节点的 Zookeeper 集群。

 

步骤1: 准备可执行文件

$ wget http://apache.is.co.za/zookeeper/zookeeper-3.6.1/apache-zookeeper-3.6.1.tar.gz
$ tar -xvf zookeeper-3.6.1.tar.gz
$ chown hadoop:hadoop -R  zookeeper-3.6.1

步骤2:切换到 hadoop 账号

su hadoop

步骤3:准备配置文件 conf/zoo.cfg,填写配置,举例如下:

tickTime=2000
dataDir=/var/data/zookeepe
clientPort=2181

步骤4:增加 myid 文件

echo 1 > /var/data/zookeeper/myid

步骤5:启动 Zookeeper 进程

$ bin/zkServer.sh start

后续,我们可以用该 Zookeeper 存储数据迁移任务信息。

 

二、定义迁移任务

 

在任务迁移数据前,需要定义迁移任务。迁移任务信息定义在 xml 文件中。具体包含如下信息:

  • 源集群,包含数据分片信息

  • 目的集群,包含数据分片信息

  • 执行数据迁移任务的线程数量

  • 定义待迁移的表信息,有 tables 字段指定,包括:

- 数据源集群名称,由 cluster_pull 指定

- 数据源数据库名称,由 database_pull 指定

- 数据源表名称,由 table_pull 指定

- 目的集群名称,由 cluster_push 指定

- 目的数据库名称,由 database_push 指定

- 目的表名称,由 table_push 指定

- 目的表引擎定义,由 engine 指定

- 待迁移的 partition 列表,由 enabled_partitions 指定。未指定,则全表迁移

 

如果目标集群数据库不存在,则不会自动创建。故迁移数据前,确保目标集群数据库存在。源表和目标表的 Schema 相同,表引擎可以不相同。

举例如下:

<yandex>
    <!-- Configuration of clusters as in an ordinary server config -->
    <remote_servers>
        <source_cluster>
            <shard>
                <internal_replication>false</internal_replication>
                    <replica>
                        <host>172.16.0.72</host>
                        <port>9000</port>
                    </replica>
            </shard>
        </source_cluster>

        <destination_cluster>
            <shard>
                <internal_replication>false</internal_replication>
                    <replica>
                        <host>172.16.0.115</host>
                        <port>9000</port>
                    </replica>
                    <replica>
                        <host>172.16.0.47</host>
                        <port>9000</port>
                    </replica>
            </shard>
            <shard>
                <internal_replication>false</internal_replication>
                    <replica>
                        <host>172.16.0.138</host>
                        <port>9000</port>
                    </replica>
                    <replica>
                        <host>172.16.0.49</host>
                        <port>9000</port>
                    </replica>
            </shard>
        </destination_cluster>
    </remote_servers>

    <!-- How many simultaneously active workers are possible. If you run more workers superfluous workers will sleep. -->
    <max_workers>8</max_workers>

    <!-- Setting used to fetch (pull) data from source cluster tables -->
    <settings_pull>
        <readonly>1</readonly>
    </settings_pull>

    <!-- Setting used to insert (push) data to destination cluster tables -->
    <settings_push>
        <readonly>0</readonly>
    </settings_push>

    <settings>
        <connect_timeout>300</connect_timeout>
        <!-- Sync insert is set forcibly, leave it here just in case. -->
        <insert_distributed_sync>1</insert_distributed_sync>
    </settings>

    <tables>
        <!-- A table task, copies one table. -->
        <table_lineorder>
            <!-- Source cluster name (from <remote_servers/> section) and tables in it that should be copied -->
            <cluster_pull>source_cluster</cluster_pull>
            <database_pull>default</database_pull>
            <table_pull>lineorder</table_pull>

            <!-- Destination cluster name and tables in which the data should be inserted -->
            <cluster_push>destination_cluster</cluster_push>
            <database_push>default</database_push>
            <table_push>lineorder_7</table_push>

            <engine>
            ENGINE=ReplicatedMergeTree('/clickhouse/tables/{shard}/lineorder_7','{replica}')
            PARTITION BY toYear(LO_ORDERDATE)
            ORDER BY (LO_ORDERDATE, LO_ORDERKEY)
            </engine>

            <!-- Sharding key used to insert data to destination cluster -->
            <sharding_key>rand()</sharding_key>

            <!-- Optional expression that filter data while pull them from source servers -->
            <!-- <where_condition></where_condition> -->
           <!--
            <enabled_partitions>
            </enabled_partitions>
           -->
        </table_lineorder>
    </tables>
</yandex>

  

准备完成配置文件后,在 Zookeeper 上准备路径,并将定义任务文件上传到 Zookeeper 中。假设配置文件为 task.xml , 执行如下指令:

$ bin/zkCli.sh create /clickhouse/copytasks ""
$ bin/zkCli.sh create /clickhouse/copytasks/task ""
$ bin/zkCli.sh create /clickhouse/copytasks/task/description "`cat ./task.xml`"

  

三、启动任务

 

定义好迁移任务后,就可以启动 clickhouse-copier 来迁移数据了。在此之前,需要准备配置文件, 配置文件中描述了 Zookeeper 地址,以及日志配置。举例如下:

<yandex>
    <logger>
        <level>trace</level>
        <size>100M</size>
        <count>3</count>
    </logger>

    <zookeeper>
        <node index="1">
            <host>172.16.0.139</host>
            <port>2181</port>
        </node>
    </zookeeper>
</yandex>

假设该文件命名为 config.xml

可以使用如下命令启动 clickhouse-copier:

 $ clickhouse-copie
  --config ./config.xml \
  --task-path /clickhouse/copytasks/task \
  --base-dir ./clickhouse \

其中,--task-path 指定数据迁移任务在 Zookeeper 上的路径,即第3节中创建的路径。需要注意的是,路径下必须包含 description 文件。

如果数据量比较多,可以部署多个 clickhouse-copier 并发执行迁移任务。

 

总结

 

clickhouse-copier 是 ClickHouse 发行版自带的工具,在稳定性可靠性上是有保证的。在使用过程中,需要注意的问题:

    • 在迁移过程中,源集群的表需要停止写入;

    • 在迁移过程中,占用源,目的集群网络带宽,需要仔细评估;

    • clickhouse-copier 提供了较多灵活性,包括数据分片算法,指定迁移表的 partitions

    •  


       

       

      关注“腾讯云大数据”公众号,技术交流、最新活动、服务专享一站Get~

推荐阅读