本地备份

本文为您介绍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操作时间,则查询结果相当于没有执行过DropRestore操作时保持一致。即Restore操作恢复的数据不会认为是新增的数据。

    • DropRestore操作依然会产生对应的Commit时间和Version。对于分区表而言,Restore整张表时,每个分区会单独Restore,从而产生独立的Version。

  • 不支持自动备份和恢复物化视图、Object Table、外表。

操作命令

备份与恢复功能涉及的操作命令如下表所示。

场景

命令

功能

权限说明

设置备份数据保留周期

setproject odps.timemachine.retention.days=days;

设置备份数据的保留天数。在此期间,您可以将当前版本恢复至任意一个备份的数据版本。

days的取值范围为[0,30],默认值为1。0代表关闭备份功能。

调整备份周期后的生效策略为:

  • 延长备份周期:新的备份周期于当日开始生效。

  • 缩短备份周期:系统将自动删除超过保留周期的备份数据。

仅阿里云账号或项目管理员可以操作。

setproject;

通过MaxCompute客户端执行此命令打印项目级别的参数信息,MaxCompute客户端使用请参见使用本地客户端(odpscmd)连接。您可以查看odps.timemachine.retention.days参数的取值。例如odps.timemachine.retention.days=1,代表当前项目备份数据的保留周期为1天(24小时)。

查看备份数据

show history for tables [like <table_name>];

查看当前项目内的表和处于备份状态的表信息,包括已经删除表,支持根据表名进行过滤,与show tables;命令不相同。

您需要具备ProjectList权限。

权限详情请参见MaxCompute权限

show history for table <table_name>[LIMIT <limit_value>] (LSN <lsn_value> | OFFSET <offset_value>;

查看指定表的备份数据,支持通过使用LIMIT指定返回记录长度,OFFSET或版本(LSN)指定起始位置。

详细参数说明请参考参数说明

  • 如果表存在,您需要具备TableShowHistory权限。

  • 如果表已删除,执行该命令会报错,您可以通过show history for tables [like <table_name>];命令查找已经删除的表及备份的数据版本信息。

权限详情请参见MaxCompute权限

show history for table table_name ('id'='xxxx');

查看已删除表的备份数据,获取保留周期内备份的各个数据版本信息。

您可以通过show history for tables [like <table_name>];命令查找已经删除的表,获取表名和表ID信息。

您需要具备ProjectList权限。

权限详情请参见MaxCompute权限

show history for table table_name partition_spec;

查看指定分区的备份数据,获取保留周期内备份的各个数据版本信息。

无。

show history for table table_name PARTITION('id'='xxxx');

查看已删除分区的备份数据,获取保留周期内备份的各个数据版本信息。id可以通过show history for table <table_name>;命令返回结果中的ObjectId字段获取。

恢复数据

restore table table_name ('id'='xxxxx');

恢复已删除的表。

您可以通过show history for tables [like <table_name>];命令查找已经删除的表,获取表名和表ID信息。

  • 执行表恢复操作时:

    • 如果表存在,您需要具备TableUpdate权限。

    • 如果表不存在,您需要具备ProjectCreateTable权限。

      权限详情请参见MaxCompute权限

    说明

    不支持聚簇表(cluster table)。

  • 执行分区恢复操作时,只能指定1LSN,即备份版本。

    当一次性恢复多个分区时,MaxCompute会将每个分区都恢复至指定LSN。如果某个分区不存在指定的LSN,MaxCompute会将该分区恢复至其第1LSN。

    例如,pt1LSN分别为100、102、104106,pt2LSN分别为101、103、104105,执行命令如下。

    restore table table_name PARTITION(pt='1') PARTITION(pt='2') to LSN '102';

    执行结果为:pt1恢复至102版本,pt2恢复至101版本。

restore table table_name to LSN 'xxxx';

恢复表至指定版本。

您可以通过show history for table <table_name>;命令获取表的版本信息。

restore table table_name to LSN 'xxxx' as new_table_name;

恢复表至指定版本,并命名为新表或将数据更新到不同名的表中。

restore table table_name PARTITION('id'='xxxx')[PARTITION('id'='xxxx')];

恢复已删除的指定分区,支持一次恢复多个分区。通常用于恢复因执行drop partition操作或被生命周期回收后,需要恢复的分区。

restore table table_name partition_spec1[partition_spec2 ]to LSN 'xxxx';

恢复指定分区至指定版本,支持一次恢复多个分区。通常用于恢复因执行overwritemerge操作后,需要恢复的分区。

restore table table_name partition_spec1[partition_spec2 ]to LSN 'xxxx' as new_table_name;

恢复指定分区至指定版本,并命名为新表。

数据文件清理操作

相对于普通表而言,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         | 
+------------+------------+------------+------------+