This topic describes how to use ApsaraDB for ClickHouse to query data in Object Storage Service (OSS) and how to write data to OSS.
Prerequisites
- OSS is activated. For information about how to activate OSS, see Activate OSS.
- An OSS bucket is created in the region where the ApsaraDB for ClickHouse cluster is deployed. For information about how to create an OSS bucket, see Create buckets.
- A file that contains data is uploaded to OSS. For information about how to upload
a file to OSS, see Upload objects.
Note In this example, the following content is stored in a file named test.csv:
1,tick,32,shanghai,http://example.com
. - The Alibaba Cloud account that you use to access the OSS bucket is granted the required permissions to read and write objects in the bucket. For information about how to grant permissions, see Overview.
Precautions
- ApsaraDB for ClickHouse allows you to access OSS files in multiple formats except Protobuf and CapnProto. For more information, see Formats for Input and Output Data.
- Make sure that your OSS bucket and ApsaraDB for ClickHouse cluster are deployed in the same region, and you can access OSS over a virtual private cloud (VPC).
- If you use ApsaraDB for ClickHouse to write data to OSS, original data in OSS files is overwritten.
Step 1: Log on to the database
- Log on to the ApsaraDB for ClickHouse console.
- On the Clusters page, click the Default Instances tab, and click the ID of the cluster that you want to manage.
- On the Cluster Information page, click Log On to Database in the upper-right corner.
- In the Log on to Database Instance dialog box, enter the username and password of the database account, and click Login.
Step 2: Create an OSS external table
Step 3: Write data to OSS
insert into oss_test_tbl values(11, 'tick', 25, 'shanghai', 'http://example.com');
Step 4: Query the data in OSS
ApsaraDB for ClickHouse allows you to use a table engine or table function to query the data in OSS.
- Use a table engine to query the data in OSS.
Query the data in the OSS external table.
select * from oss_test_tbl;
The following result is returned:┌─id─┬─user_name─┬──age──┬───city─────┬─────access_url────────┐ │ 11 │ tick │ 25 │ shanghai │ http://example.com │ └────┴───────────┴───────┴────────────┴───────────────────────┘
- Use a table function to query the data in OSS.
- For a cluster of version 21.8 and earlier, use the following syntax to query the data
in OSS:
SELECT * FROM oss('<oss-endpoint>', '<access-key-id>', '<access-key-secret>', '<oss-file-path>', '<file-format-name>', '<col_name> <col_type>(,...)');
- For a cluster of version 22.8, use the following syntax to query the data in OSS:
SELECT * FROM oss('https://<bucketName>.<oss-endpoint>/<file-name>','<access-key-id>', '<access-key-secret>', '<file-format-name>', '<col_name> <col_type>(,...)');
For more information about parameters, see the table that describes parameters.
Sample statements:- For a cluster of version 21.8 and earlier, the following statements provide an example
on how to query the data in OSS:
SELECT * FROM oss('oss-cn-shanghai-internal.aliyuncs.com', 'LTAI5tDVcUKu2CbcBwhr****', 'WAcroHBcL7IVOK8FIHzNJy91Lc****', 'oss://testBucketName/test.csv', 'CSV', 'id UInt8, user_name String, age UInt16, city String, access_url String');
- For a cluster of version 22.8, the following statements provide an example on how
to query the data in OSS:
SELECT * FROM oss('http://testBucketName.oss-cn-shanghai-internal.aliyuncs.com/test.csv', 'LTAI5tDVcUKu2CbcBwhr****', 'WAcroHBcL7IVOK8FIHzNJy91Lc****', 'CSV', 'id UInt8, user_name String, age UInt16, city String, access_url String')
The following result is returned:┌─id─┬─user_name─┬──age──┬───city─────┬─────access_url────────┐ │ 11 │ tick │ 25 │ shanghai │ http://example.com │ └────┴───────────┴───────┴────────────┴───────────────────────┘
- For a cluster of version 21.8 and earlier, use the following syntax to query the data
in OSS: