clickhouse-copier

Copies data from the tables in one cluster to tables in another (or the same) cluster.

Warning

To get a consistent copy, the data in the source tables and partitions should not change during the entire process.

You can run multiple clickhouse-copier instances on different servers to perform the same job. ZooKeeper is used for syncing the processes.

After starting, clickhouse-copier:

  • Connects to ZooKeeper and receives:

    • Copying jobs.
    • The state of the copying jobs.
  • It performs the jobs.

    Each running process chooses the “closest” shard of the source cluster and copies the data into the destination cluster, resharding the data if necessary.

clickhouse-copier tracks the changes in ZooKeeper and applies them on the fly.

To reduce network traffic, we recommend running clickhouse-copier on the same server where the source data is located.

Running Clickhouse-copier

The utility should be run manually:

  1. $ clickhouse-copier --daemon --config zookeeper.xml --task-path /task/path --base-dir /path/to/dir

Parameters:

  • daemon — Starts clickhouse-copier in daemon mode.
  • config — The path to the zookeeper.xml file with the parameters for the connection to ZooKeeper.
  • task-path — The path to the ZooKeeper node. This node is used for syncing clickhouse-copier processes and storing tasks. Tasks are stored in $task-path/description.
  • task-file — Optional path to file with task configuration for initial upload to ZooKeeper.
  • task-upload-force — Force upload task-file even if node already exists.
  • base-dir — The path to logs and auxiliary files. When it starts, clickhouse-copier creates clickhouse-copier_YYYYMMHHSS_<PID> subdirectories in $base-dir. If this parameter is omitted, the directories are created in the directory where clickhouse-copier was launched.

Format of Zookeeper.xml

  1. <yandex>
  2. <logger>
  3. <level>trace</level>
  4. <size>100M</size>
  5. <count>3</count>
  6. </logger>
  7. <zookeeper>
  8. <node index="1">
  9. <host>127.0.0.1</host>
  10. <port>2181</port>
  11. </node>
  12. </zookeeper>
  13. </yandex>

Configuration of Copying Tasks

  1. <yandex>
  2. <!-- Configuration of clusters as in an ordinary server config -->
  3. <remote_servers>
  4. <source_cluster>
  5. <!--
  6. source cluster & destination clusters accept exactly the same
  7. parameters as parameters for the usual Distributed table
  8. see https://clickhouse.com/docs/en/engines/table-engines/special/distributed/
  9. -->
  10. <shard>
  11. <internal_replication>false</internal_replication>
  12. <replica>
  13. <host>127.0.0.1</host>
  14. <port>9000</port>
  15. <!--
  16. <user>default</user>
  17. <password>default</password>
  18. <secure>1</secure>
  19. -->
  20. </replica>
  21. </shard>
  22. ...
  23. </source_cluster>
  24. <destination_cluster>
  25. ...
  26. </destination_cluster>
  27. </remote_servers>
  28. <!-- How many simultaneously active workers are possible. If you run more workers superfluous workers will sleep. -->
  29. <max_workers>2</max_workers>
  30. <!-- Setting used to fetch (pull) data from source cluster tables -->
  31. <settings_pull>
  32. <readonly>1</readonly>
  33. </settings_pull>
  34. <!-- Setting used to insert (push) data to destination cluster tables -->
  35. <settings_push>
  36. <readonly>0</readonly>
  37. </settings_push>
  38. <!-- Common setting for fetch (pull) and insert (push) operations. Also, copier process context uses it.
  39. They are overlaid by <settings_pull/> and <settings_push/> respectively. -->
  40. <settings>
  41. <connect_timeout>3</connect_timeout>
  42. <!-- Sync insert is set forcibly, leave it here just in case. -->
  43. <insert_distributed_sync>1</insert_distributed_sync>
  44. </settings>
  45. <!-- Copying tasks description.
  46. You could specify several table task in the same task description (in the same ZooKeeper node), they will be performed
  47. sequentially.
  48. -->
  49. <tables>
  50. <!-- A table task, copies one table. -->
  51. <table_hits>
  52. <!-- Source cluster name (from <remote_servers/> section) and tables in it that should be copied -->
  53. <cluster_pull>source_cluster</cluster_pull>
  54. <database_pull>test</database_pull>
  55. <table_pull>hits</table_pull>
  56. <!-- Destination cluster name and tables in which the data should be inserted -->
  57. <cluster_push>destination_cluster</cluster_push>
  58. <database_push>test</database_push>
  59. <table_push>hits2</table_push>
  60. <!-- Engine of destination tables.
  61. If destination tables have not be created, workers create them using columns definition from source tables and engine
  62. definition from here.
  63. NOTE: If the first worker starts insert data and detects that destination partition is not empty then the partition will
  64. be dropped and refilled, take it into account if you already have some data in destination tables. You could directly
  65. specify partitions that should be copied in <enabled_partitions/>, they should be in quoted format like partition column of
  66. system.parts table.
  67. -->
  68. <engine>
  69. ENGINE=ReplicatedMergeTree('/clickhouse/tables/{cluster}/{shard}/hits2', '{replica}')
  70. PARTITION BY toMonday(date)
  71. ORDER BY (CounterID, EventDate)
  72. </engine>
  73. <!-- Sharding key used to insert data to destination cluster -->
  74. <sharding_key>jumpConsistentHash(intHash64(UserID), 2)</sharding_key>
  75. <!-- Optional expression that filter data while pull them from source servers -->
  76. <where_condition>CounterID != 0</where_condition>
  77. <!-- This section specifies partitions that should be copied, other partition will be ignored.
  78. Partition names should have the same format as
  79. partition column of system.parts table (i.e. a quoted text).
  80. Since partition key of source and destination cluster could be different,
  81. these partition names specify destination partitions.
  82. NOTE: In spite of this section is optional (if it is not specified, all partitions will be copied),
  83. it is strictly recommended to specify them explicitly.
  84. If you already have some ready partitions on destination cluster they
  85. will be removed at the start of the copying since they will be interpeted
  86. as unfinished data from the previous copying!!!
  87. -->
  88. <enabled_partitions>
  89. <partition>'2018-02-26'</partition>
  90. <partition>'2018-03-05'</partition>
  91. ...
  92. </enabled_partitions>
  93. </table_hits>
  94. <!-- Next table to copy. It is not copied until previous table is copying. -->
  95. </table_visits>
  96. ...
  97. </table_visits>
  98. ...
  99. </tables>
  100. </yandex>

clickhouse-copier tracks the changes in /task/path/description and applies them on the fly. For instance, if you change the value of max_workers, the number of processes running tasks will also change.