從MaxCompute批量導入導出
HybridDB for MySQL支持直接從MaxCompute中導入和導出數據,這也是HybridDB for MySQL的特色功能之一。相比較借助D2、CDP、DTS等工具,HybridDB for MySQL直通導入和導出節省了大量的中間轉換,導入導出的速度要快10倍以上。
建議
導入導出:采用本章介紹的批量方式,即Insert overwrite方式,批量可見,性能好,異常時可冪等重試。
任務執行:建議采用Submit job方式異步執行,具體操作請參見異步提交導入任務。異步執行的方式可避免長連接超時導致的任務中斷等情況。
從MaxCompute中導入
將數據從MaxCompute導入到HybridDB for MySQL需要如下四個步驟:
準備MaxCompute中的源數據表。
在HybridDB for MySQL中建立一張外表映射到MaxCompute的源表。
在HybridDB中建立接收數據的真實存儲表。
調用
INSERT [OVERWRITE] INTO SELECT
來完成數據導入。
操作步驟
準備MaxCompute的源數據表。
這一步是準備MaxCompute源表數據,如果源表數據已經存在,可以忽略這一步。
# MaxCompute非分區表 CREATE TABLE IF NOT EXISTS odps_nopart_import_test ( uid STRING COMMENT '', other STRING COMMENT '' ) COMMENT '' LIFECYCLE 3; # MaxCompute分區表 CREATE TABLE IF NOT EXISTS odps_part_import_test ( uid STRING COMMENT '', other STRING COMMENT '' ) COMMENT '' PARTITIONED BY (ds STRING COMMENT '天') LIFECYCLE 3; # MaxCompute二級分區表 CREATE TABLE IF NOT EXISTS odps_two_part_import_test ( uid STRING COMMENT '', other STRING COMMENT '' ) COMMENT '' PARTITIONED BY (ds STRING COMMENT '天',other string) LIFECYCLE 3;
在HybridDB for MySQL中創建一張外部映射表。
這一步的作用是建立映射表,用于告訴HybridDB for MySQL如何讀取MaxCompute源表數據。
use exdb ##exdb是HybriDB for MySQL中用戶數據庫的名稱 # MaxCompute非分區表對應的外表 CREATE TABLE IF NOT EXISTS odps_nopart_import_test_external_table ( uid string, other string ) ENGINE='ODPS' TABLE_PROPERTIES='{ "endpoint":"http://service.odps.aliyun-inc.com/api", "accessid":"xxx", "accesskey":"xxx", "project_name":"xxx", "table_name":"odps_nopart_import_test" }' # MaxCompute分區表對應的外表 CREATE TABLE IF NOT EXISTS odps_part_import_test_external_table ( uid string, other string, ds string ) ENGINE='ODPS' TABLE_PROPERTIES='{ "endpoint":"http://service.odps.aliyun-inc.com/api", "accessid":"xxx", "accesskey":"xxx", "project_name":"xxx", "table_name":"odps_part_import_test", "partition_column":"ds" }' # MaxCompute二級分區表對應的外表 CREATE TABLE IF NOT EXISTS odps_two_part_import_test_external_table ( uid string, other string, ds string ) ENGINE='ODPS' TABLE_PROPERTIES='{ "endpoint":"http://service.odps.aliyun-inc.com/api", "accessid":"xxx", "accesskey":"xxx", "project_name":"xxx", "table_name":"odps_part_import_test", "partition_column":"ds,other" }'
上表定義說明如下:
ENGINE=ODPS
:表示該表是外部表,存儲引擎是外部的MaxCompute。TABLE_PROPERTIES
:用于告訴HybridDB for MySQL如何訪問MaxCompute中源表的數據。endpoint
:MaxCompute的數據連接地址,請注意公共云和阿里集團內部是不一樣的。accessid
:訪問MaxCompute源表的用戶AccessKeyID。accesskey
:訪問MaxCompute源表的用戶AccessKey。project_name
:MaxCompute中源表所在的Project名稱。tablename
:MaxCompute中源表名稱。
MaxCompute非分區表和分區表的外表定義不一樣:
外表定義(
CREATE TABLE
)多了一個ds string
定義。表屬性(
TABLE_PROPERTIES
)多了一個"partition_column":"ds"
定義。
在HybridDB for MySQL中創建一張真實數據表。
這一步是創建目標表,用于接收從MaxCompute中導入的源表數據。
# 對應MaxCompute非分區表 CREATE TABLE IF NOT EXISTS hybriddb_nopart_import_test ( uid string, other string ) DISTRIBUTE BY HASH(uid) INDEX_ALL='Y' ENGINE='CSTORE' # 對應MaxCompute分區表 CREATE TABLE IF NOT EXISTS hybriddb_part_import_test ( uid string, other string, ds string ) DISTRIBUTE BY HASH(uid) PARTITION BY VALUE(ds) INDEX_ALL='Y' ENGINE='CSTORE' # 對應MaxCompute二級分區表 CREATE TABLE IF NOT EXISTS hybriddb_two_part_import_test ( uid string, other string, ds string ) DISTRIBUTE BY HASH(uid) PARTITION BY VALUE(ds) INDEX_ALL='Y' ENGINE='CSTORE'
執行SQL語句開始導入。
可以通過兩種方式來完成導入:
方式1:實時ETL導入,實時可見。
# 實時導入MaxCompute非分區表 insert into hybriddb_nopart_import_test select * from odps_nopart_import_test_external_table # 實時導入MaxCompute分區表某個分區數據 insert into hybriddb_part_import_test select * from hybriddb_part_import_test_external_table where ds = '20170101' # 實時導入MaxCompute分區表某個二級分區數據 insert into hybriddb_two_part_import_test select * from hybriddb_two_part_import_test_external_table where ds = '20170101' and other='hangzhou'
方式2:批量ETL導入,批量可見,性能較好。
# 批量導入MaxCompute非分區表 insert overwrite into hybriddb_nopart_import_test select * from odps_nopart_import_test_external_table # 批量導入MaxCompute分區表某個分區數據 insert overwrite into hybriddb_part_import_test select * from odps_part_import_test_external_table where ds = '20170101' # 批量導入MaxCompute分區表某個二級分區數據 insert overwrite into hybriddb_part_import_test select * from odps_part_import_test_external_table where ds = '20170101' and other='hangzhou'
導出到MaxCompute
將數據從MaxCompute導出到HybridDB for MySQL需要如下四個步驟:
準備MaxCompute的真實數據表
在HybridDB for MySQL中建立一張目標外表映射到MaxCompute的數據表
準備HybridDB for MySQL的數據源表
調用
INSERT [OVERWRITE] INTO SELECT
來完成數據導出
操作步驟
準備MaxCompute的真實數據表。
# MaxCompute非分區表 CREATE TABLE IF NOT EXISTS odps_nopart_export_test ( uid STRING COMMENT '', other STRING COMMENT '' ) COMMENT '' LIFECYCLE 3; # MaxCompute分區表 CREATE TABLE IF NOT EXISTS odps_part_export_test ( uid STRING COMMENT '', other STRING COMMENT '' ) COMMENT '' PARTITIONED BY (ds STRING COMMENT '天') LIFECYCLE 3;
在HybridDB for MySQL中建立一張外表映射到MaxCompute的數據表。
這一步的作用是建立映射表,用于告訴HybridDB for MySQL如何將數據寫入到MaxCompute中。
use exdb #exdb是HybriDB for MySQL中用戶數據庫的名稱 # MaxCompute非分區表對應的外表 CREATE TABLE IF NOT EXISTS odps_nopart_export_test_external_table ( uid string, other string ) ENGINE='ODPS' TABLE_PROPERTIES='{ "endpoint":"http://service.odps.aliyun-inc.com/api", "accessid":"xxx", "accesskey":"xxx", "project_name":"xxx", "table_name":"odps_nopart_export_test" }' # MaxCompute分區表對應的外表 CREATE TABLE IF NOT EXISTS odps_part_export_test_external_table ( uid string, other string, ds string ) ENGINE='ODPS' TABLE_PROPERTIES='{ "endpoint":"http://service.odps.aliyun-inc.com/api", "accessid":"xxx", "accesskey":"xxx", "project_name":"xxx", "table_name":"odps_part_export_test", "partition_column":"ds" }'
在HybridDB for MySQL準備數據源表。
這一步是準備HybridDB for MySQL源表數據,如果源表數據已經存在,可以忽略這一步。
# 對應MaxCompute非分區表 CREATE TABLE IF NOT EXISTS hybriddb_nopart_export_test ( uid string, other string ) DISTRIBUTE BY HASH(uid) INDEX_ALL='Y' ENGINE='CSTORE' # 對應MaxCompute分區表 CREATE TABLE IF NOT EXISTS hybriddb_part_export_test ( uid string, other string, ds string ) DISTRIBUTE BY HASH(uid) PARTITION BY VALUE(ds) INDEX_ALL='Y' ENGINE='CSTORE'
執行SQL語句開始導出。
# 導出HybridDB for MySQL數據到MaxCompute非分區表 insert [overwrite] into odps_nopart_export_test_external_table select * from hybriddb_nopart_export_test # 導出HybridDB for MySQL數據到MaxCompute分區表某個分區 insert [overwrite] into odps_part_export_test_external_table select * from hybriddb_part_export_test where ds = '20170101'
注意事項
要求三表的DDL定義完全一致(MaxCompute源表、HybridDB for MySQL外表、HybridDB for MySQL存儲表)。
公共云VPC網絡不支持這種外表方式導入,需要用戶自己借助CDP/DataX等方式。
這里導入導出都是同步模式,發起導入的MySQL Client需要一直與數據庫保持連接。如需要異步提交,請參見異步提交導入任務。
將MaxCompute分區表導入HybridDB for MySQL二級分區表時,單次并行可以導入的MaxCompute分區數目為32個。
將MaxCompute非分區表導入HybridDB for MySQL二級分區表時,單次最多可以寫入的分區數目為4個。
使用OVERWRITE關鍵字表示分區覆蓋,與MaxCompute到 HybridDB for MySQL的批量ETL導入語義一致,也是批量可見。