This article explains how to use the clickhouse-client command-line tool to import local data into Alibaba Cloud ClickHouse.
Prerequisites
-
You have created a database account. For more information, see Manage accounts of Community-Compatible Edition clusters and Manage accounts of Enterprise Edition clusters.
-
The clickhouse-client tool is installed. The tool's version must be the same as or newer than your cluster's version. For more information, see Install the clickhouse-client.
NoteImporting data over the public network is slow. For large datasets or to test import performance, import data over the internal network.
To import data over an internal network, install clickhouse-client on an ECS instance within the same VPC as your Alibaba Cloud ClickHouse cluster. When you connect to the cluster, use the VPC endpoint.
To install clickhouse-client, the ECS instance must have access to the public network.
-
If you have not purchased an ECS instance, select Public IP Address when you purchase the instance. For more information, see Create an instance using the wizard.
-
If your existing ECS instance lacks a public IP, associate an EIP with it. For more information, see Associate an Elastic IP Address (EIP).
-
-
The IP address of the server where clickhouse-client is installed must be in the whitelist of your Alibaba Cloud ClickHouse cluster. For instructions, see Set Whitelist.
-
The source file must be in a supported format. For more information about the supported formats and their text requirements, see Supported file formats.
Procedure
This example demonstrates how to import a CSV file into the test_tbl_distributed distributed table in the default database of Alibaba Cloud ClickHouse. Replace the example parameters with your actual values. The environment for this example is as follows:
-
Destination database: default
-
Destination table: test_tbl_distributed
-
Source data file: testData.csv
Step 1: Prepare the data
In the installation directory of the clickhouse-client tool, create a file named testData.csv and add the following data to it.
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 is integrated with Data Management Service (DMS), a visual tool for connecting to your cluster. For more information, see Connect to a ClickHouse cluster using DMS.
If you use a different client, see Connect to a database.
-
Create a table based on your cluster edition.
ImportantThe order and data type of the columns in the table must match those in the source file to prevent import failures.
For an Enterprise Edition cluster, you only need to create a local table. For a Community-Compatible Edition cluster, you might also need to create a distributed table, depending on your setup. The following examples show the required statements. For more information about the
CREATE TABLEsyntax, see CREATE TABLE.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
The engines for single-replica and two-replica clusters are different. Select an engine based on the replica type of your cluster.
ImportantUsing a non-Replicated engine prevents replication between the replicas and can 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.
If you only need to import the file into a local table, you can skip this step.
For multi-node clusters, creating a distributed table is recommended.
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.
If you only need to import the file into a local table, you can skip this step.
For multi-node clusters, creating a distributed table is recommended.
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
In the installation directory of the clickhouse-client tool, run the following command.
To speed up the import, split the source file into parts and run multiple client processes to import them in parallel.
For multi-node clusters, importing data into a distributed table is recommended.
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 path of the source file. |
|
host |
The public endpoint or VPC endpoint of the cluster. You can find the endpoints on the Cluster Information page. Select an endpoint based on where your clickhouse-client tool is located:
To apply for a public endpoint for a cluster, see Apply for or release a public endpoint. |
|
port |
The TCP port number. You can find the port number on the Cluster Information page. |
|
user |
The database account. |
|
password |
The password of the database account. |
|
table_name |
The name of the destination table. If you created a distributed table, specify the name of the distributed table. |
|
file_type |
The format of the source data file. |
Step 4: Verify the import result
-
Connect to the cluster.
To connect to the cluster with DMS, see Connect to a ClickHouse cluster using DMS.
-
Run a query statement.
ImportantOn a multi-node Community-Compatible Edition cluster, query the distributed table to view all imported data. If you query a local table, you will see data from only a single node, resulting in an incomplete dataset.
SELECT * FROM test_tbl_local;The query returns the following result:
+--------------+---------------------+---------------+----------------+----------------------+ | 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
The following table describes common supported file formats for data import.
-
In the text file, each line represents one row in the destination table. The data in each column must correspond to the columns you specified when creating the table.
-
For formats that include headers, the parser ignores header rows containing column names or data types. The import process relies on the column order in the destination table, not the names or types in the file's header.
|
Format |
Text requirements |
Example |
|
TabSeparated |
|
|
|
TabSeparatedWithNames |
This format is similar to TabSeparated, but the first row contains column names. This header row is ignored during parsing. |
|
|
TabSeparatedWithNamesAndTypes |
This format is similar to TabSeparated, except that the first row contains column names and the second row contains data types. Both rows are ignored during parsing. |
|
|
CSV |
|
|
|
CSVWithNames |
This format is similar to CSV, except that the first row contains column names. This header row is ignored during parsing. |
|
For more information about file formats, see Formats for Input and Output Data.
References
For additional migration solutions, see Data migration and synchronization.