Skip to Content
Data SynchronizationData Import between ClickHouse

Data Import Between ClickHouse Clusters

When two UClickHouse clusters need to synchronize data and are in the same availability zone.

Download the clickhouse-client client.

Rebuild Table Structure

View the database list of the source ClickHouse cluster.

clickhouse-client --host="<old host>" --port="<old port>" --user="<old user name>" --password="<old password>" --query="SHOW databases" > database.list

View the table list of the source ClickHouse cluster.

clickhouse-client --host="<old host>" --port="<old port>" --user="<old user name>" --password="<old password>" --query="SHOW tables from <database_name>" > table.list

Export the table creation DDL from the source ClickHouse cluster.

clickhouse-client --host="<old host>" --port="<old port>" --user="<old user name>" --password="<old password>" --query="SHOW CREATE TABLE <database_name>.<table_name>" > table.sql

Import the table creation DDL into the target ClickHouse cluster.

clickhouse-client --host="<new host>" --port="<new port>" --user="<new user name>" --password="<new password>" < table.sql

Data Migration

Remote Data Migration

insert into <new_database>.<new_table> select * from remote('old_endpoint', <old_database>.<old_table>, '<username>', '<password>');

Parameter descriptions:

ParameterDescription
new_databaseTarget ClickHouse cluster database name
new_tableTarget ClickHouse cluster table name
old_endpointSource ClickHouse endpoint
old_databaseSource ClickHouse cluster database name
old_tableSource ClickHouse cluster table name
usernameSource ClickHouse cluster username
passwordSource ClickHouse cluster password

File Export Migration

  1. Export the source data to a CSV format file.
clickhouse-client --host="<old host>" --port="<oldport>" --user="<old user name>" --password="<old password>" --query="select * from <database_name>.<table_name> FORMAT CSV" > table.csv
  1. Import the CSV file into the target cluster.
clickhouse-client --host="<new host>" --port="<new port>" --user="<new user name>" --password="<new password>" --query="insert into <database_name>.<table_name> FORMAT CSV" < table.csv