Import data using clickhouse-client
Use clickhouse-client to import local files into Alibaba Cloud ClickHouse.
Prerequisites
-
A database account is created. Manage accounts of Community-Compatible Edition clusters | Manage accounts of Enterprise Edition clusters.
-
clickhouse-client (version ≥ your cluster version) is installed. Install the clickhouse-client.
NotePublic network imports are slow. For large datasets, use the internal network.
Install clickhouse-client on an ECS instance in the same VPC as your Alibaba Cloud ClickHouse cluster, then connect through the VPC endpoint.
The ECS instance requires public network access to install clickhouse-client.
-
If you have not purchased an ECS instance, select Public IP Address when you purchase the instance. Create an instance using the wizard.
-
If your existing ECS instance lacks a public IP, associate an EIP with it. Associate an Elastic IP Address (EIP).
-
-
The clickhouse-client server IP is added to the Alibaba Cloud ClickHouse whitelist. Set Whitelist.
-
The source file uses a supported format. Supported file formats.
Procedure
This example imports a CSV file into the test_tbl_distributed table in the default database of Alibaba Cloud ClickHouse. Replace parameters with your actual values:
-
Destination database: default
-
Destination table: test_tbl_distributed
-
Source data file: testData.csv
Step 1: Prepare the data
In the clickhouse-client installation directory, create testData.csv with the following content.
1,yang,32,shanghai,http://example.com
2,wang,22,beijing,http://example.com
3,xiao,23,shenzhen,http://example.com
4,jess,45,hangzhou,http://example.com
5,jack,14,shanghai,http://example.com
6,tomy,25,hangzhou,http://example.com
7,lucy,45,shanghai,http://example.com
8,tengyin,26,shanghai,http://example.com
9,wangli,27,shenzhen,http://example.com
10,xiaohua,37,shanghai,http://example.com
Step 2: Create a table
-
Connect to the database.
Alibaba Cloud ClickHouse integrates with Data Management Service (DMS). Connect to a ClickHouse cluster using DMS.
Other clients: Connect to a database.
-
Create a table based on your cluster edition.
ImportantColumn order and data types must match the source file.
Enterprise Edition requires only a local table. Community-Compatible Edition may also need a distributed table. CREATE TABLE syntax reference.
Enterprise edition
CREATE TABLE test_tbl_local ON cluster default ( id UInt8, user_name String, age UInt16, city String, access_url String ) ENGINE = MergeTree() ORDER BY id;If you receive the error message
ON CLUSTER is not allowed for Replicated databasewhen you run this statement, you can upgrade the minor engine version to fix the error.Community-compatible edition
Select an engine based on your cluster's replica type.
ImportantWhen you create a table in a dual-replica cluster, you must use a Replicated engine from the MergeTree engine family. If you create a table with a non-Replicated engine in a dual-replica cluster, data cannot be replicated between replicas, which may cause data inconsistency.
Single-replica
-
Create a local table.
CREATE TABLE test_tbl_local ON cluster default ( id UInt8, user_name String, age UInt16, city String, access_url String ) ENGINE = MergeTree() ORDER BY id; -
(Optional) Create a distributed table.
Skip this step if you only need a local table.
Recommended for multi-node clusters.
CREATE TABLE test_tbl_distributed ON cluster default ( id UInt8, user_name String, age UInt16, city String, access_url String ) ENGINE = Distributed(default, default, test_tbl_local, rand());
Two-replica
-
Create a local table.
CREATE TABLE test_tbl_local ON cluster default ( id UInt8, user_name String, age UInt16, city String, access_url String ) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{database}/{table}/{shard}', '{replica}') order by id; -
(Optional) Create a distributed table.
Skip this step if you only need a local table.
Recommended for multi-node clusters.
CREATE TABLE test_tbl_distributed ON cluster default ( id UInt8, user_name String, age UInt16, city String, access_url String ) ENGINE = Distributed(default, default, test_tbl_local, rand());
-
Step 3: Import data
Run the following command from the clickhouse-client installation directory.
To speed up imports, split the source file and run multiple clients in parallel.
For multi-node clusters, import into the distributed table.
cat <file_name> | ./clickhouse-client --host=<host> --port=<port> --user=<user> --password=<password> --query="INSERT INTO <table_name> FORMAT <file_type>";
|
Parameter |
Description |
|
file_name |
The source file path. |
|
host |
The public or VPC endpoint of the cluster, available on the Cluster Information page. Choose an endpoint based on the clickhouse-client location:
|
|
port |
The TCP port, available on the Cluster Information page. |
|
user |
The database account. |
|
password |
The password of the database account. |
|
table_name |
The destination table name. For distributed tables, use the distributed table name. |
|
file_type |
The source file format. |
Step 4: Verify the import result
-
Connect to the cluster.
-
Run a query statement.
ImportantOn multi-node Community-Compatible Edition clusters, query the distributed table to view all data. A local table returns data from one node only.
SELECT * FROM test_tbl_local;Expected output:
+--------------+---------------------+---------------+----------------+----------------------+ | id | user_name | age | city | access_url | +--------------+---------------------+---------------+----------------+----------------------+ | 1 | yang | 32 | shanghai | http://example.com | | 2 | wang | 22 | beijing | http://example.com | | 3 | xiao | 23 | shenzhen | http://example.com | | 4 | jess | 45 | hangzhou | http://example.com | | 5 | jack | 14 | shanghai | http://example.com | | 6 | tomy | 25 | hangzhou | http://example.com | | 7 | lucy | 45 | shanghai | http://example.com | | 8 | tengyin | 26 | shanghai | http://example.com | | 9 | wangli | 27 | shenzhen | http://example.com | | 10 | xiaohua | 37 | shanghai | http://example.com | +--------------+---------------------+---------------+----------------+----------------------+
Supported file formats
Common supported file formats:
-
Each line represents one row. Column order must match the table definition.
-
For formats with headers, header rows are ignored. Import relies on column order, not header names or types.
|
Format |
Text requirements |
Example |
|
TabSeparated |
|
|
|
TabSeparatedWithNames |
Same as TabSeparated, but the first row contains column names (ignored during parsing). |
|
|
TabSeparatedWithNamesAndTypes |
Same as TabSeparated, but the first row contains column names and the second row contains data types. Both rows are ignored during parsing. |
|
|
CSV |
|
|
|
CSVWithNames |
Same as CSV, but the first row contains column names (ignored during parsing). |
|
Full format reference: Formats for Input and Output Data.
References
Other migration methods: Data migration and synchronization.