將 PrestoDB 對接 Tablestore 前,需完成 Presto Server 的 Catalog 配置和 Schema 配置。對接完成後,可參考本文的 SQL 樣本進行資料操作。
Server配置
根據需要對 Presto Server 進行 Catalog 配置和 Schema 配置。
Catalog 配置定義連接器設定和 Schema 配置模式。Schema 配置模式分為 Meta 表動態配置和本地靜態檔案配置兩種。
使用 Meta 表動態配置時,啟動 Presto Server 前無需配置任何 Schema 資訊,只需在啟動 SQL CLI 後通過 SQL 命令建立 Schema 和映射表。
Catalog配置
Catalog 設定檔位於 PrestoDB 安裝目錄下的etc/catalog/tablestore.properties。
配置樣本如下:
connector.name=tablestore
tablestore.schema-mode=meta-table
#tablestore.schema-mode=file
tablestore.schema-file=/users/test/tablestore/presto/tablestore.schema
tablestore.meta-instance=metastoreinstance
tablestore.endpoint=http://metastoreinstance.cn-hangzhou,ots.aliyuncs.com/
tablestore.accessid=****************
tablestore.accesskey=**************************
tablestore.meta-table=meta_table
tablestore.auto-create-meta-table=true
配置項說明如下:
|
配置項 |
樣本 |
是否必選 |
說明 |
|
connector.name |
tablestore |
是 |
連接器名稱,必須設定為 |
|
tablestore.schema-mode |
meta-table |
是 |
Schema 配置模式。取值如下:
|
|
tablestore.schema-file |
/users/test/tablestore/presto/tablestore.schema |
是,有條件 |
僅當 本地靜態檔案的完整路徑。詳見Schema配置。 |
|
tablestore.meta-instance |
metastoreinstance |
是,有條件 |
僅當 用於儲存中繼資料的 Tablestore 執行個體,請根據實際修改。更多資訊,請參見執行個體。 重要
請確保配置的執行個體已在阿里雲帳號中存在。 |
|
tablestore.endpoint |
http://metastoreinstance.cn-hangzhou,ots.aliyuncs.com/ |
是,有條件 |
僅當 用於儲存中繼資料的 Tablestore 執行個體的訪問地址,請根據實際修改。更多資訊,請參見服務地址。 |
|
tablestore.accessid |
|
是,有條件 |
僅當 對中繼資料執行個體有存取權限的阿里雲帳號或RAM使用者的AccessKey ID和AccessKey Secret。 |
|
tablestore.accesskey |
|
是,有條件 |
|
|
tablestore.meta-table |
meta_table |
是,有條件 |
僅當 用於儲存中繼資料的 Tablestore 表名,請根據實際修改。 |
|
tablestore.auto-create-meta-table |
true |
否 |
僅當 是否自動建立中繼資料表。預設值為 |
Schema配置
Schema 配置支援 Meta 表動態配置和本地靜態檔案配置兩種方式,推薦使用 Meta 表動態配置。
Meta表動態配置
本地靜態檔案配置
本地靜態檔案的路徑必須與 Catalog 配置中 tablestore.schema-file 指定的路徑一致。
在伺服器上建立一個檔案,寫入如下配置:
{
"account" : {
"accessId" : "xxxxxxxx", ----- <必選>阿里雲帳號或RAM使用者的AccessKey ID。
"accessKey" : "xxxxxxxxxxxxxxx", ----- <必選>阿里雲帳號或RAM使用者的AccessKey Secret。
},
"instances" : {
"mydb" : { ----- <必選>PrestoDB中Schema名稱。
"instanceName" : "myinstance", ----- <必選>Tablestore執行個體的名稱。
"endpoint" : "http://myinstance.cn-hangzhou.ots.aliyuncs.com", ----- <必選>Tablestore執行個體的Endpoint。
"tables": {
"mytable" : { ----- <必選>PrestoDB中表的名稱。
"originName" : "SampleTable", ----- <可選>對應tablestore中表的名稱(注意大小寫),如果不配置,則與Presto中表名一致。
"columns" : [ ----- <必選> 表的中繼資料,必須包含所有的主鍵列,且主鍵列順序必須與表的主鍵順序一致。
{"name" : "gid", "type" : "bigint"},
{"name" : "uid", "type" : "bigint"},
{"name" : "c1", "type" : "boolean", "originName" : "col1"},
{"name" : "c2", "type" : "bigint", "originName" : "col2"},
{"name" : "C3", "type" : "varchar", "originName" : "col3"}
]
},
"anotherTable" : {
"originName" : "sampleTable2",
"columns" : [
{"name" : "gid", "type" : "bigint"},
{"name" : "uid", "type" : "bigint"},
{"name" : "a", "type" : "varchar"},
{"name" : "b", "type" : "varchar"},
{"name" : "c", "type" : "boolean"},
{"name" : "d", "type" : "bigint"},
{"name" : "e", "type" : "varchar"}
]
}
}
}
}
}
|
配置項 |
是否必選 |
說明 |
|
|
account |
accessId |
是 |
阿里雲帳號或RAM使用者的AccessKey ID和AccessKey Secret。 |
|
accessKey |
是 |
||
|
instances(map(<schema_name> -> <schema_info>)) |
是 |
Schema 名稱到 Schema 資訊的JSON Map 配置。 重要
Schema 名稱在 PrestoDB 中大小寫不敏感。 |
|
|
<schema_info> |
instanceName |
是 |
Schema 對應的 Tablestore 執行個體名稱。更多資訊,請參見執行個體。 |
|
endpoint |
是 |
Tablestore 執行個體的服務地址。更多資訊,請參見服務地址。 |
|
|
tables(map(<table_name> -> <table_info>)) |
是 |
掛載到 PrestoDB 中的表列表。 重要
|
|
|
<table_info> |
originName |
否 |
對應 Tablestore 中實際的表名稱。
若 connector 在 Tablestore 中找不到對應的表,則無法對該表執行任何讀寫操作。 |
|
columns(list([<column_info>])) |
是 |
表的中繼資料,需包含所有定義的列。 重要
|
|
|
<column_info> |
name |
是 |
列名。 重要
列名在 PrestoDB 中大小寫不敏感。 |
|
type |
是 |
列類型。PrestoDB 中映射到 Tablestore 列類型的支援類型為 Bigint、Varchar、Varbinary、Double 和 Boolean。 重要
PrestoDB 中定義的列類型必須與 Tablestore 中的列類型匹配。 |
|
|
originName |
否 |
對應 Tablestore 執行個體中的列名。 |
|
常用SQL樣本
以下樣本展示如何通過 PrestoDB 操作 Tablestore 資料。所有樣本基於名為 testdb 的 Schema(串連到 Tablestore 執行個體 myinstance)和名為 exampletable 的映射表(表結構為 (pk varchar, c1 double, c2 varchar, c3 varchar))。
建立Schema
Schema 用於配置 Tablestore 執行個體的訪問資訊並進行鑒權。以下樣本建立名為 testdb 的 Schema,用於訪問 Tablestore 執行個體 myinstance。
CREATE SCHEMA tablestore.testdb
WITH (
endpoint = 'https://myinstance.cn-hangzhou.ots.aliyuncs.com',
instance_name = 'myinstance',
access_id = '************************',
access_key = '********************************'
);
參數說明如下:
|
參數 |
樣本 |
是否必選 |
說明 |
|
endpoint |
https://myinstance.cn-hangzhou.ots.aliyuncs.com |
是 |
Tablestore 執行個體的訪問地址。更多資訊,請參見服務地址。 |
|
instance_name |
myinstance |
是 |
Tablestore 執行個體名稱。更多資訊,請參見執行個體。 |
|
access_id |
**** |
是 |
阿里雲帳號或RAM使用者的AccessKey ID。 |
|
access_key |
** |
是 |
阿里雲帳號或RAM使用者的AccessKey Secret。 |
建立映射表
映射表對應 Tablestore 執行個體中的物理表。
建立映射表時,請注意以下事項:
映射表中的欄位類型必須與 Tablestore 資料表中的欄位類型匹配。更多資訊,請參見欄位類型映射。
映射表名稱必須與 Tablestore 中實際的表名稱相同。
CREATE TABLE 語句中的
table_name用於映射到 Tablestore 中的實際表。可以為同一個 Tablestore 資料表建立多個不同名稱的映射表。映射表必須包含資料表的所有主鍵列,但可以只包含部分屬性列。
映射表中主鍵列的名稱和順序必須與 Tablestore 資料表中保持一致。每個屬性列可通過
origin_name參數映射到 Tablestore 中實際的列名。
以下樣本為 Tablestore 資料表 exampletable 建立同名映射表。
CREATE TABLE if not exists exampletable
(
pk varchar,
c1 double with (origin_name = 'col1'),
c2 varchar with (origin_name = 'col2'),
c3 varchar with (origin_name = 'col3')
) WITH (
table_name = 'exampletable'
);
Schema相關操作
建立 Schema 後,可查看已有 Schema 列表或切換到指定 Schema。
擷取Schema列表
查看已建立的 Schema 列表。
show schemas;
返回樣本如下:
Schema
--------------------
information_schema
testdb
testdb1
(3 rows)
使用Schema
切換到指定 Schema 後,才能操作該 Schema 中的表。
只有切換到 Schema 後,才能對其中的表執行操作。
以下樣本切換到 testdb Schema。
use testdb;
返回樣本如下:
USE
映射表相關操作
建立映射表後,可查看指定 Schema 中的表列表、查看錶的描述資訊或刪除映射表。
擷取Schema中表列表
切換到 Schema 後,查看已建立的表列表。
show tables;
返回樣本如下:
Table
-----------------
exampletable
sampletable
sampletabletest
table1
testtable
(5 rows)
查看錶的描述資訊
以下樣本查看 exampletable 的描述資訊。
describe exampletable;
返回樣本如下:
Column | Type | Extra | Comment
--------+---------+-------+---------
pk | varchar | |
c1 | double | |
c2 | varchar | |
c3 | varchar | |
(4 rows)
刪除映射表
以下樣本刪除映射表 table1。
drop table table1;
返回樣本如下:
DROP TABLE
資料操作
建立映射表後,可向 Tablestore 表寫入資料,或對其中的資料進行查詢和分析。
當前不支援通過 PrestoDB 更新或刪除 Tablestore 表中的資料。
寫入資料
-
插入一行資料
以下樣本向
exampletable寫入一行資料。insert into exampletable values('101', 22.0, 'Mary', '10002');返回樣本如下:
INSERT: 1 row -
大量匯入資料
重要大量匯入資料前,請確保目標表已存在,且表結構與來源資料表保持一致。
以下樣本將
exampletable中c1小於 50 的行資料複製到sampletable。insert into sampletable select pk, c1, c2, c3 from exampletable where c1 < 50;返回樣本如下:
INSERT: 3 row
讀取資料
-
查詢表中所有資料
以下樣本讀取
exampletable表中的所有資料。select * from exampletable;返回樣本如下:
pk | c1 | c2 | c3 -----+------+------+------- 100 | 11.0 | Lily | 10001 101 | 22.0 | Mary | 10002 102 | 12.0 | Jim | 10003 (3 rows) -
查詢滿足條件的資料
以下樣本查詢
exampletable中c1小於 15 且c3等於10001的行資料。select * from exampletable where c1 < 15 and c3 = '10001';返回樣本如下:
pk | c1 | c2 | c3 -----+------+------+------- 100 | 11.0 | Lily | 10001 (1 row)
分析資料
-
計算指定列的平均值
以下樣本計算
exampletable中c1列的平均值。select avg(c1) as Average from exampletable;返回結果如下:
Average --------- 15.0 (1 row) -
統計表的總行數
以下樣本統計
exampletable的總行數。select count(*) as total from exampletable;返回樣本如下:
total ------- 3 (1 row)