前言
大資料計算服務 MaxCompute(原名 ODPS)是一種快速、完全托管的EB級資料倉庫解決方案。随着資料收集手段不斷豐富,行業資料大量積累,資料規模已增長到了傳統軟體行業無法承載的海量資料(TB、PB、EB)級别。MaxCompute 緻力于批量結構化資料的存儲和計算,提供海量資料倉庫的解決方案及分析模組化服務。它具有大規模計算存儲、多種計算模型、強資料安全、低成本、免運維、極緻彈性擴充的優勢。
可以将 Tablestore 中的資料于 MaxCompute 進行對接,後續,利用 MaxCompute 強大的計算能力,對資料進行進一步的消費和計算。
下面,本文将逐漸說明如何将 Tablestore 中的資料導入 MaxCompute 中。
準備工作
需要開通
MaxCompute服務,并
建立工作空間。
這裡建立的工作空間命名為 test_tablestore_odps。

資料導入
我們将訂單系統中的 order_contract 表同步至 MaxCompute。MaxCompute 提供了多種方式讀取 tablestore 中的資料,這裡我們采用先建構外部表,然後再通過 SQL 根據外部表建立内部表進而投遞資料。
建立外部表
以管理者身份登入
DataWorks控制台。選擇區域,在左側導航欄,單擊工作空間清單。
點選工作空間“test_tablestore_odps”右側的進入資料開發。
在臨時查詢下建立節點選擇 ODPS SQL。
輸入節點名稱,點選送出。
在頁面中輸入建外表語句:
CREATE EXTERNAL TABLE IF NOT EXISTS order_max_compute
(
odps_id string,
create_time string,
pay_time bigint,
has_paid bigint,
c_id string,
c_name string,
p_brand string,
p_count bigint,
p_id string,
p_name string,
p_price double,
s_id string,
s_name string,
total_price double
)
STORED BY 'com.aliyun.odps.TableStoreStorageHandler'
WITH SERDEPROPERTIES (
'tablestore.columns.mapping'=':oId,create_time,pay_time,has_paid,c_id,c_name,p_brand,p_count,p_id,p_name,p_price,s_id,s_name,total_price',
'tablestore.table.name'='order_contract',
'odps.properties.rolearn'='acs:ram::1831126559450753:role/aliyunodpsdefaultrole'
)
LOCATION 'tablestore://test-20210609.cn-hangzhou.ots-internal.aliyuncs.com';
其中 LOCATION 填入 Tablestore 經典網位址。odps.properties.rolearn 填入 RAM 中 AliyunODPSDefaultRole 的 ARN 資訊。具體可參考:
OTS外部表。點選運作按鈕,完成外表建立。
檢視外部表
在臨時查詢的 SQL 視窗中輸入
select * from order_max_compute limit 10000
可以看到查詢結果
建立内部表
建立外部表後,Tablestore的資料便引入到了 MaxCompute 生态中。但此時隻是通過映射關系讀取資料,資料還未存儲在 MaxCompute 中。是以如果有需求需要反複讀取資料,将相比每次從 Tablestore 去遠端讀資料,更高效的方法是先一次性把需要的資料導入到 MaxCompute 内部成為一個 MaxCompute(内部)表。可以利用外部表和 SQL 直接實作該需求。執行如下 SQL 建立内部表。
CREATE TABLE order_max_compute_internal AS
SELECT * FROM order_max_compute;
檢視内部表
在
DataWorks管理控制台首頁,點選工作空間清單,點選對應工作空間進入資料地圖。點選我的資料,在清單中可以看到 MaxCompute 中的 order_max_computer_internal 表,點選進入。
點選資料預覽,可以看到導入的資料。
總結
本文展示了如何将 Tablestore 中的資料導入到 MaxCompute(ODPS)。這為後續更為便捷的資料分析、資料計算操作提供了可能性。