Import data using clickhouse-client

更新时间:
复制 MD 格式

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.

    Note

    Importing 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.

  • 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

  1. 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.

  2. Create a table based on your cluster edition.

    Important

    The 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 TABLE syntax, 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 database when 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.

    Important

    Using a non-Replicated engine prevents replication between the replicas and can cause data inconsistency.

    Single-replica

    1. 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;
    2. (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

    1. 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;
    2. (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:

  • On an Alibaba Cloud ECS instance:

    If the instance is in the same VPC as the Alibaba Cloud ClickHouse cluster: Use the VPC endpoint. Data import over the internal network is faster.

    If the instance is in a different VPC from the Alibaba Cloud ClickHouse cluster: Use the public endpoint.

  • On a server other than an Alibaba Cloud ECS instance, use the public endpoint.

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

  1. Connect to the cluster.

    To connect to the cluster with DMS, see Connect to a ClickHouse cluster using DMS.

  2. Run a query statement.

    Important

    On 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.

Important
  • 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

  • Each row of data is written on a new line, with columns separated by tabs.

  • You must escape tab characters, newline characters, and backslashes as \t, \n, and \\, respectively.

  • NULL values are represented by \N.

John\t28\tSoftware Engineer\n
Alice\t32\tData Analyst\n
Bob\t25\tProduct Manager\n

TabSeparatedWithNames

This format is similar to TabSeparated, but the first row contains column names. This header row is ignored during parsing.

Name\tAge\tOccupation\n
John\t28\tSoftware Engineer\n
Alice\t32\tData Analyst\n
Bob\t25\tProduct Manager\n

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.

Name\tAge\tOccupation\n
String\tUInt16\tString\n
John\t28\tSoftware Engineer\n
Alice\t32\tData Analyst\n
Bob\t25\tProduct Manager\n

CSV

  • Each row of data is written on a new line.

  • Escape a double quote within a string by using two double quotes. For example, the text "Hello, World!" is written as ""Hello, World!"" in a file.

  • Numeric values are not enclosed in double quotation marks.

  • The default column delimiter is ,. You can also use the --format_csv_delimiter parameter to specify a different column delimiter when you execute an import command. For example, if columns are separated by a vertical bar, the command is as follows.

    cat testData.csv | ./clickhouse-client --format_csv_delimiter="|" --host=cc-bp163l724nkf8****.clickhouse.ads.aliyuncs.com --port=3306 --user=test --password=123456Aa --query="INSERT INTO test_tbl_distributed FORMAT CSV";
John,28,Software Engineer
Alice,32,Data Analyst
Bob,25,Product Manager

CSVWithNames

This format is similar to CSV, except that the first row contains column names. This header row is ignored during parsing.

id,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

For more information about file formats, see Formats for Input and Output Data.

References

For additional migration solutions, see Data migration and synchronization.