本文為您介紹MaxCompute的備份與恢複功能和操作命令,並提供參考樣本。
概述
MaxCompute提供資料備份與恢複功能,系統會自動備份資料的歷史版本(例如被刪除或修改前的資料)並保留一定時間,您可以對保留周期內的資料進行快速恢複,避免因誤操作遺失資料。
備份與恢複功能具備以下特點:
預設開啟,不需要手動開通
該功能不依賴外部儲存,系統預設為所有MaxCompute專案開放的資料保留周期為24小時,備份和儲存免費。
自動持續備份
系統自動對發生變更的資料進行備份,多次變更時將備份多個資料版本,相比固定周期性的備份策略,可以有效避免因誤操作遺失資料。
恢複快速,操作簡單
MaxCompute具備先進的中繼資料和多資料版本管理能力,備份和恢複操作不佔用額外的計算資源,您可以通過命令快速恢複不同規模的資料。
注意事項
使用備份恢複功能時,您需要注意如下事項:
如果您通過MaxCompute用戶端查看錶的備份資訊,建議您下載最新版本的MaxCompute用戶端,歷史版本的用戶端可能無法查看到備份恢複參數及表的備份資訊。
對錶執行一次修改操作就會產生一個備份版本。如果未對錶執行修改操作,不會產生備份版本。
當某個備份版本的保留天數超過專案設定的備份資料保留天數時,MaxCompute會自動刪除備份版本。被刪除的備份版本無法恢複,也無法查詢到。
如果表執行過Purge命令後,資料將無法恢複。
已經刪除的表或分區(包含Delta Table),無法直接通過恢複到指定版本的方式進行恢複,需要先恢複表或分區後,再恢複到對應的版本 。
Delta Table使用限制:
Restore操作只支援完整恢複已Drop的表或者分區,不支援Restore到具體的小版本,如想查詢歷史小版本資料可通過timetravel查詢擷取。
已Drop的表或者分區,通過Restore恢複後,對於TimeTravel查詢與Incremental查詢的行為和沒Drop前基本保持一致,但以下細節需要注意:
Drop後的表,在沒有Restore恢複前,包括timetravel在內的任何查詢都是不支援的,會直接報錯;Drop後的分區,任何查詢都無法查詢到資料;和常設查詢行為保持一致。
Drop後的表,在Restore恢複後,如果timetravel / incremental查詢的時間小於Restore操作時間,大於等於Drop操作時間,則認為資料已經刪除,查詢不到資料。
Drop後的表,在Restore恢複後,如果timetravel / incremental查詢的時間小於Drop操作時間,則查詢結果相當於沒有執行過Drop操作時保持一致。
Drop後的表,在Restore恢複後,如果timetravel / incremental查詢的時間大於等於Restore操作時間,則查詢結果相當於沒有執行過Drop和Restore操作時保持一致。即Restore操作恢複的資料不會認為是新增的資料。
Drop和Restore操作依然會產生對應的Commit時間和Version。對於分區表而言,Restore整張表時,每個分區會單獨Restore,從而產生獨立的Version。
不支援自動備份和恢複物化視圖、Object Table、外表。
操作命令
備份與恢複功能涉及的操作命令如下表所示。
情境 | 命令 | 功能 | 許可權說明 |
設定備份資料保留周期 |
| 設定備份資料的保留天數。在此期間,您可以將目前的版本恢複至任意一個備份的資料版本。
調整備份周期後的生效策略為:
| 僅阿里雲帳號或專案系統管理員可以操作。 |
| 通過MaxCompute用戶端執行此命令列印專案層級的參數資訊,MaxCompute用戶端使用請參見使用本地用戶端(odpscmd)串連。您可以查看odps.timemachine.retention.days參數的取值。例如 | ||
查看備份資料 |
| 查看當前專案內的表和處於備份狀態的表資訊,包括已經刪除表,支援根據表名進行過濾,與 | 您需要具備Project的List許可權。 許可權詳情請參見MaxCompute許可權。 |
| 查看指定表的備份資料,支援通過使用LIMIT指定返回記錄長度,OFFSET或版本(LSN)指定起始位置。 詳細參數說明請參考參數說明。 |
許可權詳情請參見MaxCompute許可權。 | |
| 查看已刪除表的備份資料,擷取保留周期內備份的各個資料版本資訊。 您可以通過 | 您需要具備Project的List許可權。 許可權詳情請參見MaxCompute許可權。 | |
| 查看指定分區的備份資料,擷取保留周期內備份的各個資料版本資訊。 | 無。 | |
| 查看已刪除分區的備份資料,擷取保留周期內備份的各個資料版本資訊。id可以通過 | ||
恢複資料 |
| 恢複已刪除的表。 您可以通過 |
|
| 恢複表至指定版本。 您可以通過 | ||
| 恢複表至指定版本,並命名為新表或將資料更新到不同名的表中。 | ||
| 恢複已刪除的指定分區,支援一次恢複多個分區。通常用於恢複因執行 | ||
| 恢複指定分區至指定版本,支援一次恢複多個分區。通常用於恢複因執行 | ||
| 恢複指定分區至指定版本,並命名為新表。 |
資料檔案清理操作
相對於普通表而言,Delta Table會額外保留Time Travel時間周期內的歷史資料,當然也需要支付額外的儲存成本,其他情境,系統會在合適的時機(最長不超過一天)自動刪除不需要的歷史資料檔案,使用者不需要額外操作,也可節省對應的儲存成本,可刪除的歷史資料包括:
早於timetravel時間周期的歷史資料。如果表配置了不支援Time Travel查詢,所有的歷史資料都可自動刪除。
超過了生命週期或者執行Drop操作後,並且也超過了備份保護時間的歷史資料。
歷史資料需滿足上面所有的條件,才可被自動刪除。
此處的歷史資料特指已經遷移到資源回收筒目錄的資料檔案,一般情況下,除了超過生命週期或者執行Drop操作外,執行Compaction或者insert overwrite也會導致當前資料寫入目錄的存量資料檔案遷移至資源回收筒目錄,等待系統自動刪除。
檔案強制清理文法
--手動強制移除資源回收筒的歷史資料檔案 PURGE TABLE <table_name>;使用說明與限制
執行此命令後,系統會立即刪除資源回收筒中的所有的歷史資料,可能導致timetravel操作無法查詢歷史資料。
一般只在特殊情境中執行此命令應急,比如檔案太多導致磁碟讀寫不穩定,歷史資料太多導致成本暴增等。正常情況下,資源回收筒中的歷史資料等待系統自動刪除即可。
使用樣本
CREATE TABLE mf_ttt (pk BIGINT NOT NULL PRIMARY KEY, val BIGINT NOT NULL) tblproperties ("transactional"="true"); INSERT INTO TABLE mf_ttt VALUES (1, 1), (2, 2); INSERT INTO TABLE mf_ttt VALUES (2, 20), (3, 3); SELECT * FROM mf_ttt version AS OF 2; -- 返回結果。 +------------+------------+ | pk | val | +------------+------------+ | 1 | 1 | | 2 | 2 | +------------+------------+ -- 執行compaction-> purge -> timetravel查詢。 ALTER TABLE mf_ttt compact major; SELECT * FROM mf_ttt version AS OF 2; -- 返回結果。 +------------+------------+ | pk | val | +------------+------------+ | 1 | 1 | | 2 | 2 | +------------+------------+ PURGE TABLE mf_ttt; SELECT * FROM mf_ttt version AS OF 2; --返回結果。 +------------+------------+ | pk | val | +------------+------------+ | 1 | 1 | | 3 | 3 | | 2 | 20 | +------------+------------+
查看備份資料樣本
為您介紹如何查看錶的備份資料。
查看所有表的備份資料。
執行
SHOW HISTORY FOR tables;命令,樣本如下。-- 建立表test_restore_x。 CREATE TABLE test_restore_1(colname STRING); CREATE TABLE test_restore_2(colname INT); -- 查看所有表的備份資料。 SHOW HISTORY FOR tables;返回結果如下。
ID = 20250718083243873gj6aesnzawr3 Name Id Type IsPartitioned CreateTime DropTime test_restore_1 a0b06367bd054d17a55505aa31601b89 MANAGED_TABLE FALSE 2025-07-18 16:32:13 test_restore_2 437b29466ba948b392b2090ec0a60fc2 MANAGED_TABLE FALSE 2025-07-18 16:32:14 OK查看所有表的備份資料,支援根據表名進行過濾。
執行
SHOW HISTORY FOR tables [LIKE <table_name>];命令,樣本如下。-- 查看錶test_restore1的備份資料。 SHOW HISTORY FOR tables LIKE test_restore_1;返回結果如下。
ID = 20250718083704294gcw24sgigrl Name Id Type IsPartitioned CreateTime DropTime test_restore_1 a0b06367bd054d17a55505aa31601b89 MANAGED_TABLE FALSE 2025-07-18 16:32:13 OK查看指定表的備份資料,擷取保留周期內備份的各個資料版本資訊。
執行
SHOW HISTORY FOR table <table_name>;命令,樣本如下。-- 更新表test_restore_1資料。 INSERT OVERWRITE TABLE test_restore_1 values("0"); INSERT OVERWRITE TABLE test_restore_1 values("1"); INSERT OVERWRITE TABLE test_restore_1 values("2"); INSERT OVERWRITE TABLE test_restore_1 values("3"); -- 查看錶test_restore_1的備份資料。 SHOW HISTORY FOR TABLE test_restore_1;返回結果如下。
ID = 2025071808522592gbougy83ad1 ObjectType ObjectId ObjectName VERSION(LSN) Time Operation TABLE a0b06367bd054d17a55505aa31601b89 test_restore_1 0000000000000001 2025-07-18 16:32:14 CREATE TABLE a0b06367bd054d17a55505aa31601b89 test_restore_1 0000000000000002 2025-07-18 16:52:08 OVERWRITE TABLE a0b06367bd054d17a55505aa31601b89 test_restore_1 0000000000000003 2025-07-18 16:52:12 OVERWRITE TABLE a0b06367bd054d17a55505aa31601b89 test_restore_1 0000000000000004 2025-07-18 16:52:14 OVERWRITE TABLE a0b06367bd054d17a55505aa31601b89 test_restore_1 0000000000000005 2025-07-18 16:52:17 OVERWRITE OK查看指定表的備份資料,擷取保留周期內備份的各個資料版本資訊,並指定起始位置及長度。
執行
SHOW HISTORY FOR table <table_name> [LIMIT <limit_value>] (LSN <lsn_value> | OFFSET <offset_value>);命令,樣本如下。SHOW HISTORY FOR TABLE test_restore_1 LIMIT 1 LSN '00000000000000000002';返回結果如下。
ID = 20250718090511323gzfl5f9glr2 ObjectType ObjectId ObjectName VERSION(LSN) Time Operation TABLE a0b06367bd054d17a55505aa31601b89 test_restore_1 0000000000000002 2025-07-18 16:52:08 OVERWRITE OK查看已刪除表的備份資料。
執行
SHOW HISTORY FOR TABLE table_name ('id'='xxxx');命令,樣本如下。-- 更新表test_restore_2資料。 INSERT OVERWRITE TABLE test_restore_2 values(0); INSERT OVERWRITE TABLE test_restore_2 values(1); INSERT OVERWRITE TABLE test_restore_2 values(2); INSERT OVERWRITE TABLE test_restore_2 values(3); -- 刪除表test_restore_2。 DROP TABLE test_restore_2; -- 確認刪除表test_restore_2操作。 Confirm to "drop table test_restore_2;" (yes/no)? yes -- 查看刪除表test_restore_2的備份資料。 SHOW HISTORY FOR TABLE test_restore_2('id'='437b29466ba948b392b2090ec0a60fc2');返回結果如下。
ID = 20250718091226165g1vtbsnyu4h ObjectType ObjectId ObjectName VERSION(LSN) Time Operation TABLE 437b29466ba948b392b2090ec0a60fc2 test_restore_2 0000000000000001 2025-07-18 16:32:14 CREATE TABLE 437b29466ba948b392b2090ec0a60fc2 test_restore_2 0000000000000002 2025-07-18 17:12:20 DROP OK查看分區表的備份資料。
執行
SHOW HISTORY FOR TABLE table_name ('id'='xxxx');命令,查看分區表的備份資料。樣本如下。-- 建立表test_restore_part_1。 CREATE TABLE test_restore_part_1(colname string) PARTITIONED BY(ds string); -- 更新表test_restore_part_1。 INSERT OVERWRITE TABLE test_restore_part_1 partition(ds="20250701") values ("1"); INSERT OVERWRITE TABLE test_restore_part_1 partition(ds="20250702") values ("2"); INSERT OVERWRITE TABLE test_restore_part_1 partition(ds="20250703") values ("3"); INSERT OVERWRITE TABLE test_restore_part_1 partition(ds="20250704") values ("4"); INSERT OVERWRITE TABLE test_restore_part_1 partition(ds="20250705") values ("5"); INSERT OVERWRITE TABLE test_restore_part_1 partition(ds="20250706") values ("6"); INSERT OVERWRITE TABLE test_restore_part_1 partition(ds="20250101") values ("20250101"); INSERT OVERWRITE TABLE test_restore_part_1 partition(ds="20250102") values ("20250102"); -- 查看錶test_restore_part_1的備份資料。 SHOW HISTORY FOR TABLE test_restore_part_1('id'='fbee66b56cf544d2a9999d5d0ce5d352');返回結果如下。
ID = 20250718092558737ge7pyaa803e ObjectType ObjectId ObjectName VERSION(LSN) Time Operation TABLE fbee66b56cf544d2a9999d5d0ce5d352 test_restore_part_1 0000000000000001 2025-07-18 17:23:11 CREATE PARTITION 271aebf3e17a4a8e9f6b35733bf63db4 ds=20250701 0000000000000002 2025-07-18 17:25:23 CREATE PARTITION df9e7f502d464763a243fefe1da4f911 ds=20250702 0000000000000003 2025-07-18 17:25:27 CREATE PARTITION 24cf9953e8c5445bb2f1ead87ea09bae ds=20250703 0000000000000004 2025-07-18 17:25:28 CREATE PARTITION 1c4a9a608d3d4d22bc194ab402da4b8f ds=20250704 0000000000000005 2025-07-18 17:25:32 CREATE PARTITION be0d4c9ff5034c4f932d1a2245c2e65a ds=20250705 0000000000000006 2025-07-18 17:25:35 CREATE PARTITION 45957304be5f4d09a5a3ba66808604be ds=20250706 0000000000000007 2025-07-18 17:25:37 CREATE PARTITION c36933ddefb6415bacabd5f453ee3ce7 ds=20250101 0000000000000008 2025-07-18 17:25:42 CREATE PARTITION 0b01e0d234fc4bf6a3529752e3045037 ds=20250102 0000000000000009 2025-07-18 17:25:44 CREATE OK查看分區的備份資料。
執行
SHOW HISTORY FOR TABLE table_name partition_spec;或SHOW HISTORY FOR TABLE table_name PARTITION('id'='xxxx');命令,查看分區的備份資料。樣本如下。
-- 查看錶test_restore_part_1指定分區的備份資料。
SHOW HISTORY FOR TABLE test_restore_part_1('id'='fbee66b56cf544d2a9999d5d0ce5d352')
PARTITION(ds='20250701') PARTITION(ds='20250702');返回結果如下。
ID = 2025071809280096g7ou2qft0o2
ObjectType ObjectId ObjectName VERSION(LSN) Time Operation
PARTITION 271aebf3e17a4a8e9f6b35733bf63db4 ds=20250701 0000000000000002 2025-07-18 17:25:23 CREATE
PARTITION df9e7f502d464763a243fefe1da4f911 ds=20250702 0000000000000003 2025-07-18 17:25:27 CREATE
OK恢複非分區表示例
為您介紹如何恢複非分區表的資料。
恢複已刪除的表。
執行
RESTORE TABLE table_name ('id'='xxxxx');命令,樣本如下。如果存在同名的表,您需要將同名的表重新命名後才能執行恢複操作。-- 查詢已刪除表test_restore_2的備份資料。 SHOW HISTORY FOR TABLE test_restore_2('id'='437b29466ba948b392b2090ec0a60fc2'); -- 建立同名表test_restore_part_2。 CREATE TABLE test_restore_2(colname string); -- 恢複表test_restore_part_2,但因為存在同名表會報錯。 RESTORE TABLE test_restore_2('id'='437b29466ba948b392b2090ec0a60fc2'); -- 重新命名存在的同名表test_restore_part_2。 ALTER TABLE test_restore_2 rename TO test_restore_2_rename; -- 恢複已刪除的表test_restore_part_2。 RESTORE TABLE test_restore_2('id'='437b29466ba948b392b2090ec0a60fc2');返回結果顯示OK。
恢複表至指定版本。 對於已經刪除的表,需要先恢複表之後再進行此操作。
執行
RESTORE TABLE table_name TO LSN 'xxxx';命令,樣本如下。-- 恢複表test_restore_1至指定版本。 RESTORE TABLE test_restore_1 TO LSN '0000000000000004'; -- 查詢表test_restore_part_1資料。 SELECT * FROM test_restore_1;返回結果如下。
+------------+ | colname | +------------+ | 2 | +------------+恢複表至指定版本,並命名為新表或將資料更新到不同名的表中。
執行
RESTORE TABLE table_name TO LSN 'xxxx' AS new_table_name;命令,樣本如下。包含以下三種情境:
恢複表至指定版本,並命名為新表。
-- 恢複已刪除的表test_restore_2至指定版本,並命名為新表。 RESTORE TABLE test_restore_2 TO LSN '0000000000000003' AS test_restore_new; -- 查詢表test_restore_new資料。 SELECT * FROM test_restore_new;返回結果如下。
+------------+ | colname | +------------+ | 1 | +------------+恢複表至指定版本,並將資料更新到已存在的不同名表中。
-- 恢複表test_restore_2至指定版本,並更新到已存在的不同名表中。 RESTORE TABLE test_restore_2 TO LSN '0000000000000005' AS test_restore_new; -- 查詢表test_restore_new的備份資料。 SHOW HISTORY FOR TABLE test_restore_new;返回結果如下。
ID = 20250722082102262gsizjcbzawr3 ObjectType ObjectId ObjectName VERSION(LSN) Time Operation TABLE 565b5e29eecf4fbd88965b24822ded6a test_restore_new 0000000000000001 2025-07-22 16:17:1CREATE TABLE 565b5e29eecf4fbd88965b24822ded6a test_restore_new 0000000000000002 2025-07-22 16:20:5OVERWRITE OK恢複表至指定版本,並將資料更新到不同名且Schema不一致的表中。該操作執行失敗,兩個表的Schema必須要保持一致。樣本如下。
-- 建立一個Schema不一致的表。 CREATE TABLE test_restore_2cols(col1 string, col2 string); -- 恢複表test_restore_1資料至指定版本,並將資料更新到test_restore_2cols表中。 RESTORE TABLE test_restore_1 TO LSN '0000000000000005' AS test_restore_2cols;返回結果如下。
FAILED: Catalog Service Failed, ErrorCode: 105, Error Message: ODPS-0110061: Failed to run ddltask - Restore table failed because: field schema not same, [{"comment":"","id":"","name":"colname","type":"string"}] vs [{"comment":"","id":"","name":"col1","type":"string"}, {"comment":"","id":"","name":"col2","type":"string"}]
恢複分區表和分區樣本
為您介紹如何恢複分區表或分區的資料。
恢複分區表。
執行
RESTORE TABLE table_name ('id'='xxxxx');命令,樣本如下。-- 建立表test_restore_part_x。 CREATE TABLE test_restore_part_x(a string) PARTITIONED BY(ds string); -- 更新表test_restore_part_x。 INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191201") values ("1"); INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191202") values ("2"); INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191203") values ("3"); INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191204") values ("4"); INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191205") values ("5"); INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191205") values ("6"); INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20200101") values ("20200101"); INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20200102") values ("20200102"); -- 查看錶test_restore_part_x的分區。 LIST PARTITIONS test_restore_part_x; -- 查看錶test_restore_part_x的資料。 SELECT * FROM test_restore_part_x; -- 刪除表test_restore_part_x。 DROP TABLE test_restore_part_x; -- 確認刪除表test_restore_part_x操作。 Confirm to "drop table test_restore_part_x;" (yes/no)? yes -- 恢複表test_restore_part_x。 RESTORE TABLE test_restore_part_x('id'='94d436523fe14ba39f33d2dee738c018'); -- 查看錶test_restore_part_x的備份資料。 SHOW HISTORY FOR TABLE test_restore_part_x('id'='94d436523fe14ba39f33d2dee738c018'); -- 查看錶test_restore_part_x的分區。 LIST PARTITIONS test_restore_part_x;返回結果如下。
ds=20191201 ds=20191202 ds=20191203 ds=20191204 ds=20191205 ds=20200101 ds=20200102恢複分區。 對於已經刪除的表,需要先恢複表之後再進行此操作。
執行
RESTORE TABLE table_name PARTITION('id'='xxxx')[PARTITION('id'='xxxx')];命令,樣本如下。-- 建立表test_restore_part_y。 CREATE TABLE test_restore_part_y(a string) PARTITIONED BY(ds string); -- 更新表test_restore_part_y。 INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20250701") values ("1"); INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20250702") values ("2"); INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20250703") values ("3"); INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20250704") values ("4"); INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20250705") values ("5"); INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20250706") values ("6"); INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20250101") values ("20250101"); INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20250102") values ("20250102"); -- 查看錶test_restore_part_y的分區。 LIST PARTITIONS test_restore_part_y; -- 刪除表test_restore_part_y的分區。 ALTER TABLE test_restore_part_y DROP PARTITION(ds='20250701'),PARTITION(ds='20250702'); -- 確認刪除分區操作。 Confirm to "alter table test_restore_part_y drop partition(ds='20250701'),partition(ds='20250702');" (yes/no)? yes -- 查看錶test_restore_part_y的分區。 LIST PARTITIONS test_restore_part_y; -- 查看錶test_restore_part_y的分區ID。 SHOW HISTORY FOR TABLE test_restore_part_y; -- 恢複表test_restore_part_y的分區。 RESTORE TABLE test_restore_part_y PARTITION('id'='a14d6cb4ab0c46378a6e284b257bbfaa') PARTITION('id'='8c85184ec0b44fba8198274401df2519'); -- 查看錶test_restore_part_y的分區。 LIST PARTITIONS test_restore_part_y;返回結果如下。
ds=20250101 ds=20250102 ds=20250701 ds=20250702 ds=20250703 ds=20250704 ds=20250705 ds=20250706恢複分區至指定版本。 對於已經刪除的表,需要先恢複表之後再進行此操作。
執行
RESTORE TABLE table_name partition_spec1[partition_spec2] to LSN 'xxxx';命令,樣本如下。-- 更新分區表test_restore_part_y。 INSERT OVERWRITE TABLE test_restore_part_y PARTITION(ds="20250701") VALUES ("20250701_v1"); INSERT OVERWRITE TABLE test_restore_part_y PARTITION(ds="20250702") VALUES ("20250702_v1"); INSERT OVERWRITE TABLE test_restore_part_y PARTITION(ds="20250701") VALUES ("20250701_v2"); INSERT OVERWRITE TABLE test_restore_part_y PARTITION(ds="20250702") VALUES ("20250702_v2"); -- 查看test_restore_part_y指定分區的資料。 SELECT * FROM test_restore_part_y WHERE ds='20250701' or ds='20250702'; -- 恢複test_restore_part_y指定分區至指定版本。 RESTORE TABLE test_restore_part_y PARTITION(ds='20250701') PARTITION(ds='20250702') to LSN '0000000000000010'; -- 查看test_restore_part_y指定分區的資料。 SELECT * FROM test_restore_part_y WHERE ds='20250701' or ds='20250702';返回結果如下。
-- 第一次查詢返回結果。 +------------+------------+ | a | ds | +------------+------------+ | 20250701_v2 | 20250701 | | 20250702_v2 | 20250702 | +------------+------------+ -- 第二次查詢返回結果。 +------------+------------+ | a | ds | +------------+------------+ | 2 | 20250702 | | 1 | 20250701 | +------------+------------+恢複分區至指定版本,並命名為新表。 對於已經刪除的表,需要先恢複表之後再進行此操作。
執行
RESTORE TABLE table_name partition_spec1[partition_spec2] to LSN 'xxxx' as new_table_name;命令,樣本如下。-- 恢複分區至指定版本,並命名為新表test_restore_part_y_v10。 RESTORE TABLE test_restore_part_y PARTITION(ds='20250701') PARTITION(ds='20250702') TO LSN '0000000000000010' AS test_restore_part_y_v10; -- 查看新表test_restore_part_y_v10的資料。 SELECT * FROM test_restore_part_y_v10 WHERE ds='20250701' or ds='20250702';返回結果如下。
+------------+------------+ | a | ds | +------------+------------+ | 1 | 20250701 | | 2 | 20250702 | +------------+------------+
Delta Table樣本
--建立表
CREATE TABLE mf_dt (pk BIGINT NOT NULL PRIMARY KEY,
val BIGINT NOT NULL)
PARTITIONED BY (dd STRING, hh STRING)
TBLPROPERTIES ("transactional"="true");
INSERT OVERWRITE TABLE mf_dt PARTITION(dd='01', hh='01')
VALUES (1, 1), (2, 2), (3, 3);
INSERT INTO TABLE mf_dt PARTITION(dd='01', hh='01')
VALUES (3, 30), (4, 4), (5, 5);
SELECT * FROM mf_dt WHERE dd='01' AND hh='01';
-- 返回結果。
+------------+------------+------------+------------+
| pk | val | dd | hh |
+------------+------------+------------+------------+
| 1 | 1 | 01 | 01 |
| 4 | 4 | 01 | 01 |
| 5 | 5 | 01 | 01 |
| 2 | 2 | 01 | 01 |
| 3 | 30 | 01 | 01 |
+------------+------------+------------+------------+
-- 恢複表, 'table_id' 可以通過desc extended table擷取。
DESC EXTENDED mf_dt;
-- 返回結果。
+------------------------------------------------------------------------------------+
| Owner: ALIYUN$*****cloud_com |
| Project: mc_oss_external_tables |
| TableComment: |
+------------------------------------------------------------------------------------+
| CreateTime: 2025-07-21 15:31:42 |
| LastDDLTime: 2025-07-21 15:31:42 |
| LastModifiedTime: 2025-07-21 17:14:58 |
+------------------------------------------------------------------------------------+
| InternalTable: YES | Size: 8976 |
+------------------------------------------------------------------------------------+
| Native Columns: |
+------------------------------------------------------------------------------------+
| Field | Type | Label | ExtendedLabel | Nullable | DefaultValue | Comment |
+------------------------------------------------------------------------------------+
| pk | bigint | | | false | NULL | |
| val | bigint | | | false | NULL | |
+------------------------------------------------------------------------------------+
| Partition Columns: |
+------------------------------------------------------------------------------------+
| dd | string | |
| hh | string | |
+------------------------------------------------------------------------------------+
| Extended Info: |
+------------------------------------------------------------------------------------+
| TableID: cd607cd938dc4ca6886dd12212995604 |
| IsArchived: false |
| PhysicalSize: 26928 |
| FileNum: 10 |
| StoredAs: AliOrc |
| CompressionStrategy: normal |
| Transactional: true |
| IsolationMin: NONSTRICT_SNAPSHOT_ISOLATION |
| odps.timemachine.retention.days: 1 |
| encryption_enable: false |
| Primarykey: [pk] |
| acid.data.retain.hours: 24 |
| write.bucket.num: 16 |
+------------------------------------------------------------------------------------+
DROP TABLE mf_dt;
Confirm to "DROP TABLE mf_dt" (yes/no)? yes
RESTORE TABLE mf_dt ('id' = 'cd607cd938dc4ca6886dd12212995604');
SELECT * FROM mf_dt WHERE dd='01' AND hh='01';
-- 返回結果。
+------------+------------+------------+------------+
| pk | val | dd | hh |
+------------+------------+------------+------------+
| 1 | 1 | 01 | 01 |
| 4 | 4 | 01 | 01 |
| 5 | 5 | 01 | 01 |
| 2 | 2 | 01 | 01 |
| 3 | 30 | 01 | 01 |
+------------+------------+------------+------------+
-- 恢複分區,'partition_id'可通過SHOW HISTORY for TABLE。
SHOW HISTORY FOR TABLE mf_dt;
-- 返回結果。
ObjectType ObjectId ObjectName VERSION(LSN) Time Operation
TABLE cd607cd938dc4ca6886dd12212995604 mf_dt 0000000000000001 2025-07-21 15:31:4CREATE
PARTITION 51d38cc9ded344cf99188cd1a806e5d2 dd=01/hh=01 0000000000000002 2025-07-21 17:14:4CREATE
PARTITION 51d38cc9ded344cf99188cd1a806e5d2 dd=01/hh=01 0000000000000003 2025-07-21 17:14:5APPEND
TABLE cd607cd938dc4ca6886dd12212995604 mf_dt 0000000000000004 2025-07-21 17:18:4DROP
TABLE cd607cd938dc4ca6886dd12212995604 mf_dt 0000000000000005 2025-07-21 17:18:5RESTORE
PARTITION 51d38cc9ded344cf99188cd1a806e5d2 dd=01/hh=01 0000000000000006 2025-07-21 17:18:5RESTORE
ALTER TABLE mf_dt DROP PARTITION (dd = '01', hh = '01');
Confirm to "ALTER TABLE mf_dt DROP PARTITION (dd = '01', hh = '01')" (yes/no)? yes
RESTORE TABLE mf_dt PARTITION('id' = '51d38cc9ded344cf99188cd1a806e5d2');
SELECT * FROM mf_dt WHERE dd='01' AND hh='01';
-- 返回結果。
+------------+------------+------------+------------+
| pk | val | dd | hh |
+------------+------------+------------+------------+
| 1 | 1 | 01 | 01 |
| 4 | 4 | 01 | 01 |
| 5 | 5 | 01 | 01 |
| 2 | 2 | 01 | 01 |
| 3 | 30 | 01 | 01 |
+------------+------------+------------+------------+
-- timetravel查詢restore的表。
SELECT * FROM mf_dt version AS OF 2 WHERE dd = '01' AND hh = '01';
-- 返回結果。
+------------+------------+------------+------------+
| pk | val | dd | hh |
+------------+------------+------------+------------+
| 1 | 1 | 01 | 01 |
| 3 | 3 | 01 | 01 |
| 2 | 2 | 01 | 01 |
+------------+------------+------------+------------+
SELECT * FROM mf_dt version AS OF get_latest_version('mf_dt')
WHERE dd = '01' AND hh = '01';
-- 返回結果。
+------------+------------+------------+------------+
| pk | val | dd | hh |
+------------+------------+------------+------------+
| 1 | 1 | 01 | 01 |
| 4 | 4 | 01 | 01 |
| 5 | 5 | 01 | 01 |
| 2 | 2 | 01 | 01 |
| 3 | 30 | 01 | 01 |
+------------+------------+------------+------------+