本節主要介紹如何在雲資料庫ClickHouse(核心版本為19.15.2.2)中使用阿里雲MaxCompute外表方式匯入資料。
資料匯入
- 登入ClickHouse目標資料庫,建立外部表格。文法如下:
CREATE TABLE <table_name> [on cluster default] ( 'col_name' col_type,[col_name col_type] ) ENGINE = MaxCompute('<tunnel-endpoint>', '<project-name>', '<table-name>', '<partition-spec>', '<access-key-id>', '<access-key-secret>');參數名 描述 table_name 自訂外部表格名。 'col_name' col_type 列名,列類型。 建表時,ClickHouse的列類型,應該與MaxCompute列類型具有對應關係,請參見類型映射。
ENGINE = MaxCompute 表示該表是外部表格,使用的儲存引擎是MaxCompute。 tunnel-endpoint MaxCompute的EndPoint(網域名稱節點)。 說明 為了確保ClickHouse後端節點與您的MaxCompute服務之間網路通暢, tunnel-endpoint 必須是VPC網路類型的endpoint,且MaxCompute必須與您的ClickHouse執行個體在同一地區,比如都在上海Region。查看MaxCompute tunnel endpoint地址請參見Endpoint。project-name MaxCompute中資料來源所在的專案名。 說明 必須完全與MaxCompute中的名稱保持一致。table-name MaxCompute中資料來源所在的表名。 說明 必須完全與MaxCompute中的名稱保持一致。partition-spec 希望訪問的MaxCompute分區。 需要指定特定分區,在多個分區運算式之間不能有空格,且不要包含單引號,比如正確的樣本為: sale_date=2020-09-01,region=beijing 。
access-key-id 您在訪問MaxCompute上的資料來源時所持有的AccessKey ID。 說明 必須對MaxCompute中目標資料相應的專案、表有讀取許可權。access-key-secret 您在訪問MaxCompute上的資料來源時所持有的AccessKey Secret。 樣本CREATE TABLE default.odps_src_tbl on cluster default ( `shop_name` String, `customer_id` String, `total_price` Float64 ) ENGINE = MaxCompute('http://dt.cn-shanghai.maxcompute.aliyun-inc.com', 'test_project', 'test_tbl', 'sale_date=2020-09-01,region=beijing', '<your-access-key-id>', '<your-access-key-secret>'); - 建立目標表。
CREATE TABLE default.odps_dst_tbl_local on cluster default ( `shop_name` String, `customer_id` String, `total_price` Float64 ) ENGINE= MergeTree() ORDER BY customer_id; CREATE TABLE default.odps_dst_tbl_dist on cluster default as default.odps_dst_tbl_local ENGINE= Distributed(default, default, odps_dst_tbl_local, rand()); - 將外部表格中資料匯入目標表。
insert into odps_dst_tbl_dist select * from odps_src_tbl;
類型映射
| MaxCompute類型 | ClickHouse類型 |
| boolean | UInt8 |
| tinyint | UInt8, Int8 |
| smalllint | UInt16, Int16 |
| int | UInt32, Int32 |
| bigint | UInt64, Int64 |
| float | Float32 |
| double | Float64 |
| char | String |
| varchar | String |
| binary | String |
| string | String |
| date | UInt16 |
| datetime | UInt32 |
| UUID | 暫不支援 |
| INTERVAL | 暫不支援 |
| Decimal | 暫不支援 |
| Timestamp | 暫不支援 |
| 複合資料型別 | 暫不支援 |