Import data from Object Storage Service (OSS) to Alibaba Cloud ClickHouse by using a table engine or a table function to query, analyze, and process logs.
Prerequisites
-
Activate Object Storage Service (OSS). For more information, see Activate OSS.
-
A bucket is created in the same region as Alibaba Cloud ClickHouse. For instructions on how to create a bucket, see Create a bucket.
-
The account used to access OSS has read permissions on the objects in the bucket. For more information, see Overview.
Data preparation
Save the following test data as test.csv and upload it to OSS. The default column separator is a comma (,). For upload instructions, see Upload files.
1,yang,32,shanghai,http://example1.com
2,wang,22,beijing,http://example2.com
3,xiao,23,shenzhen,http://example3.com
4,jess,45,hangzhou,http://example4.com
5,jack,14,shanghai,http://example5.com
6,tomy,25,hangzhou,http://example6.com
7,lucy,45,shanghai,http://example7.com
8,tengyin,26,shanghai,http://example8.com
9,wangli,27,shenzhen,http://example9.com
10,xiaohua,37,shanghai,http://example10.com
Steps
-
Connect to the Alibaba Cloud ClickHouse cluster. For more information, see Connect to a cluster.
-
Create a local table named
oss_test_tbl_local.Important-
The schema of the Alibaba Cloud ClickHouse table must match the schema of the OSS external table and the format of the data in OSS. To prevent parsing failures and cluster exceptions, handle null fields carefully.
-
Select the appropriate CREATE TABLE statement based on the replica configuration of your cluster. You can view the replica configuration on the Cluster Information page in the Cluster Properties section of the console.
-
You can select a table engine based on your business requirements. For more information, see Table engines.
Single-replica edition
CREATE TABLE oss_test_tbl_local ON CLUSTER default ( id UInt8, user_name String, age UInt16, city String, access_url String ) ENGINE = MergeTree() ORDER BY id;Double-replica edition
CREATE TABLE oss_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 named
oss_test_tbl_distributed.NoteCreate a distributed table if you want to distribute data across all local tables in the cluster.
CREATE TABLE oss_test_tbl_distributed ON CLUSTER default ( id UInt8, user_name String, age UInt16, city String, access_url String ) ENGINE = Distributed(default, default, oss_test_tbl_local, rand()); -
Import data from OSS to Alibaba Cloud ClickHouse.
Alibaba Cloud ClickHouse supports two methods to import data from OSS: table engines and table functions.
ImportantSelect the appropriate statement based on your cluster's version. You can view the version on the Cluster Information page in the Cluster Properties section of the console.
Method 1: Use a table engine
Method 2: Use a table function
-
Query the distributed table
oss_test_tbl_distributedto verify the data import.SELECT * FROM oss_test_tbl_distributed;Expected result:
┌─id─┬─user_name─┬──age──┬───city─────┬─────access_url────────┐ │ 1 │ yang │ 32 │ shanghai │ http://example1.com │ │ 2 │ wang │ 22 │ beijing │ http://example2.com │ │ 3 │ xiao │ 23 │ shenzhen │ http://example3.com │ │ 4 │ jess │ 45 │ hangzhou │ http://example4.com │ │ 5 │ jack │ 14 │ shanghai │ http://example5.com │ │ 6 │ tomy │ 25 │ hangzhou │ http://example6.com │ │ 7 │ lucy │ 45 │ shanghai │ http://example7.com │ │ 8 │ tengyin │ 26 │ shanghai │ http://example8.com │ │ 9 │ wangli │ 27 │ shenzhen │ http://example9.com │ │ 10 │ xiaohua │ 37 │ shanghai │ http://example10.com │ └────┴───────────┴───────┴────────────┴───────────────────────┘
Wildcard matching for OSS storage paths
To simplify the analysis of multiple small files that share a naming convention, the oss-file-path parameter supports the following wildcards:
-
*: Matches any file or directory name. For example,/dir/*matches all files in the/dirdirectory. -
{x,y,z}: Matches any value enclosed in the braces. For example,file_{x,y,z}matchesfile_x,file_y, orfile_z. -
{num1..num2}: Matches any value in the expanded range fromnum1tonum2. For example,file_{1..3}matchesfile_1,file_2, andfile_3. -
?: Matches any single character. For example,file_?matchesfile_a,file_b,file_c, and so on.
Example
The uploaded files use the following directory structure.
oss://testBucketName/
doc-data/
oss-import/
small_files/
access_log_csv_1.txt
access_log_csv_2.txt
access_log_csv_3.txt
Examples:
-
oss://testBucketName/doc-data/oss-import/small_files/*
-
oss://testBucketName/doc-data/oss-import/small_files/access*
-
oss://testBucketName/doc-data/oss-import/small_files/access_log_csv_{1,2,3}.txt
-
oss://testBucketName/doc-data/oss-import/*/access_log_csv_{1,2,3}.txt
-
oss://testBucketName/doc-data/oss-import/*/*
-
oss://testBucketName/doc-data/oss-import/*/access_log_csv_{1..3}.txt
-
oss://testBucketName/doc-data/oss-import/*/access_log_csv_?.txt