本文介紹如何在同一個雲帳號下實現Tablestore和MaxCompute之間的無縫串連。
背景資訊
MaxCompute是一項MaxCompute,它能提供快速、完全託管的PB級資料倉儲解決方案,使您可以經濟並高效地分析處理海量資料。您只需通過一條簡單的DDL語句,即可在MaxCompute上建立一張外部表格,建立MaxCompute表與外部資料源的關聯,提供各種資料的接入和輸出能力。MaxCompute表是結構化的資料,而外部表格可以不限於結構化資料。
Tablestore與MaxCompute都有其自身的類型系統,兩者之間的類型對應關係如下表所示。
Tablestore | MaxCompute |
---|---|
STRING | STRING |
INTEGER | BIGINT |
DOUBLE | DOUBLE |
BOOLEAN | BOOLEAN |
BINARY | BINARY |
準備工作
使用MaxCompute訪問Tablestore前,您需要完成以下準備工作:
步驟一:安裝並配置用戶端
步驟二:建立外部表格
建立一張MaxCompute的資料表(ots_vehicle_track)關聯到Tablestore的某一張表(vehicle_track)。
關聯的資料表資訊如下。
- 執行個體名稱:cap1
- 資料表名稱:vehicle_track
- 主鍵資訊:vid(int),gt(int)
- 訪問網域名稱:
https://cap1.cn-hangzhou.ots-internal.aliyuncs.com
CREATE EXTERNAL TABLE IF NOT EXISTS ots_vehicle_track
(
vid bigint,
gt bigint,
longitude double,
latitude double,
distance double ,
speed double,
oil_consumption double
)
STORED BY 'com.aliyun.odps.TableStoreStorageHandler' -- (1)
WITH SERDEPROPERTIES ( -- (2)
'tablestore.columns.mapping'=':vid, :gt, longitude, latitude, distance, speed, oil_consumption', -- (3)
'tablestore.table.name'='vehicle_track' -- (4)
)
LOCATION 'tablestore://cap1.cn-hangzhou.ots-internal.aliyuncs.com'; -- (5)
參數說明如下:
標號 | 參數 | 說明 |
---|---|---|
(1) | com.aliyun.odps.TableStoreStorageHandler | MaxCompute內建的處理Tablestore資料的StorageHandler,定義了MaxCompute和Tablestore的互動,相關邏輯由MaxCompute實現。 |
(2) | SERDEPROPERITES | 可以理解為提供參數選項的介面,在使用TableStoreStorageHandler時,有兩個必須指定的選項,分別是tablestore.columns.mapping和tablestore.table.name。 |
(3) | tablestore.columns.mapping | 必填選項。MaxCompute將要訪問的Tablestore表的列,包括主鍵和屬性列。其中,帶: 的表示Tablestore主鍵,例如本樣本中的:vid與 :gt ,其他均為屬性列。在指定映射的時候,使用者必須提供指定Tablestore表的所有主鍵,屬性列無需全部提供,可以只提供需要通過MaxCompute來訪問的屬性列。
|
(4) | tablestore.table.name | 需要訪問的Tablestore表名。 如果指定的Tablestore表名錯誤(不存在),則會報錯。MaxCompute不會主動建立Tablestore表。 |
(5) | LOCATION | 指定訪問的Tablestore的執行個體資訊,包括執行個體名和endpoint等。 |
步驟三:通過外部表格訪問Tablestore資料
建立外部表格後,Tablestore的資料便引入到了MaxCompute生態中,您可通過MaxCompute SQL命令來訪問Tablestore資料。
//統計編號4以下的車輛在時間戳記1469171387以前的平均速度和平均油耗。
select vid,count(*),avg(speed),avg(oil_consumption) from ots_vehicle_track where vid <4 and gt<1469171387 group by vid;
返回類似如下結果: