文档

CLONE TABLE

更新时间:

当您需要将一张表中的数据复制到另一个表中时,可以使用MaxCompute的clone table功能,提高数据迁移效率。本文以具体示例为您介绍clone table功能的使用。

功能介绍

clone table支持高效地将源表中的数据复制到目标表中。将数据复制到目标表之后,建议您执行数据验证以确保复制后数据的准确性。例如,执行select命令查看表的数据、执行desc命令查看表的大小。

使用限制

  • 目标表与源表的Schema需要兼容。

  • 支持分区表和非分区表,支持对聚簇表使用clone table命令复制表数据。

  • 目标表已存在时,一次性复制分区的数量上限为10000个。

  • 目标表不存在时,无分区数量限制,满足原子性。

  • 不支持在跨地域的MaxCompute项目之间使用clone table命令复制表数据。

  • 不支持对外部表使用clone table命令复制表数据。

命令格式

clone table <[<src_project_name>.]<src_table_name>> [partition(<pt_spec>), ...]
 to <[<dest_project_name>.]<dest_table_name>> [if exists [overwrite | ignore]] ;
  • src_project_name:可选。源表所属MaxCompute项目名称。不指定时,默认为当前项目。当源表与目标表不属于同一个MaxCompute项目时,需要携带此参数。

  • src_table_name:必填。源表名称。

  • pt_spec:可选。源表的分区信息。格式为(partition_col1 = partition_col_value1, partition_col2 = partition_col_value2, ...)partition_col是分区字段,partition_col_value是分区值。

  • dest_project_name:可选。目标表所属MaxCompute项目名称。不指定时,默认为当前项目。当目标表与源表不属于同一个MaxCompute项目时,需要携带此参数。

  • dest_table_name:必填。目标表名称。

    • 当目标表不存在时,clone table命令会创建目标表,创建目标表使用的是create table like语义。更多create table like信息,请参见创建表

    • 当目标表已存在并指定if exists overwrite时,clone table命令会覆盖目标表或对应分区的数据。

    • 当目标表已存在并指定if exists ignore时,clone table命令会跳过已存在分区,不覆盖目标表已有分区的数据。

示例数据

为便于理解,本文为您提供源数据,基于源数据提供相关示例。创建分区表sale_detail和非分区表sale_detail_np,并添加数据,命令示例如下:

  • 分区表sale_detail

    --创建一张分区表sale_detail。
    create table if not exists sale_detail
    (
    shop_name     string,
    customer_id   string,
    total_price   double
    )
    partitioned by (sale_date string, region string);
    
    --向源表增加分区。
    alter table sale_detail add partition (sale_date='2013', region='china') partition (sale_date='2014', region='shanghai');
    
    --向源表追加数据。
    insert into sale_detail partition (sale_date='2013', region='china') values ('s1','c1',100.1),('s2','c2',100.2),('s3','c3',100.3);
    insert into sale_detail partition (sale_date='2014', region='shanghai') values ('null','c5',null),('s6','c6',100.4),('s7','c7',100.5);

    查询分区表sale_detail中的数据,命令示例如下:

    --开启全表扫描,仅此Session有效。执行select语句查看表sale_detail中的数据。
    set odps.sql.allow.fullscan=true; 
    select * from sale_detail;
    
    --返回结果。
    +------------+-------------+-------------+------------+------------+
    | shop_name  | customer_id | total_price | sale_date  | region     |
    +------------+-------------+-------------+------------+------------+
    | s1         | c1          | 100.1       | 2013       | china      |
    | s2         | c2          | 100.2       | 2013       | china      |
    | s3         | c3          | 100.3       | 2013       | china      |
    | null       | c5          | NULL        | 2014       | shanghai   |
    | s6         | c6          | 100.4       | 2014       | shanghai   |
    | s7         | c7          | 100.5       | 2014       | shanghai   |
    +------------+-------------+-------------+------------+------------+
  • 非分区表sale_detail_np

    --创建一张非分区表sale_detail_np。
    create table if not exists sale_detail_np
    (
    shop_name     string,
    customer_id   string,
    total_price   double
    );
    
    --向源表追加数据。
    insert into sale_detail_np values ('s4','c4',100.4);

    查询非分区表sale_detail_np中的数据,命令示例如下:

    select * from sale_detail_np;
    --返回结果。
    +------------+-------------+-------------+
    | shop_name  | customer_id | total_price |
    +------------+-------------+-------------+
    | s4         | c4          | 100.4       |
    +------------+-------------+-------------+

使用示例

基于示例数据,clone table命令的使用示例如下:

  • 示例1:全量复制非分区表sale_detail_np的数据至目标表sale_detail_np_clone。命令示例如下:

    --复制表数据。
    clone table sale_detail_np to sale_detail_np_clone;
    --查看复制后目标表sale_detail_np_clone的信息,验证数据准确性。
    select * from sale_detail_np_clone;
    --返回结果。
    +------------+-------------+-------------+
    | shop_name  | customer_id | total_price |
    +------------+-------------+-------------+
    | s4         | c4          | 100.4       |
    +------------+-------------+-------------+
  • 示例2:复制分区表sale_detail指定分区的数据至目标表sale_detail_clone。命令示例如下:

    --复制表数据。
    clone table sale_detail partition (sale_date='2013', region='china') to sale_detail_clone if exists overwrite;
    --查看复制后目标表sale_detail_clone的信息,验证数据准确性。
    select * from sale_detail_clone;
    --返回结果。
    +------------+-------------+-------------+------------+------------+
    | shop_name  | customer_id | total_price | sale_date  | region     |
    +------------+-------------+-------------+------------+------------+
    | s1         | c1          | 100.1       | 2013       | china      |
    | s2         | c2          | 100.2       | 2013       | china      |
    | s3         | c3          | 100.3       | 2013       | china      |
    +------------+-------------+-------------+------------+------------+
  • 示例3:全量复制分区表sale_detail的数据至目标表sale_detail_clone(示例2已生成的表)并跳过目标表中已存在的分区。命令示例如下:

    --复制表数据。
    clone table sale_detail to sale_detail_clone if exists ignore;
    
    --查看复制后目标表sale_detail_clone的信息,验证数据准确性。
    --开启全表扫描,仅此Session有效。执行select语句查看表sale_detail中的数据。
    set odps.sql.allow.fullscan=true; 
    select * from sale_detail_clone;
    
    --返回结果。
    +------------+-------------+-------------+------------+------------+
    | shop_name  | customer_id | total_price | sale_date  | region     |
    +------------+-------------+-------------+------------+------------+
    | s1         | c1          | 100.1       | 2013       | china      |
    | s2         | c2          | 100.2       | 2013       | china      |
    | s3         | c3          | 100.3       | 2013       | china      |
    | null       | c5          | NULL        | 2014       | shanghai   |
    | s6         | c6          | 100.4       | 2014       | shanghai   |
    | s7         | c7          | 100.5       | 2014       | shanghai   |
    +------------+-------------+-------------+------------+------------+
  • 示例4:全量复制分区表sale_detail的数据至目标表sale_detail_clone1。命令示例如下:

    --复制表数据。
    clone table sale_detail to sale_detail_clone1;
    --查看复制后目标表sale_detail_clone1的信息,验证数据准确性。
    select * from sale_detail_clone1;
    --返回结果。
    +------------+-------------+-------------+------------+------------+
    | shop_name  | customer_id | total_price | sale_date  | region     |
    +------------+-------------+-------------+------------+------------+
    | s1         | c1          | 100.1       | 2013       | china      |
    | s2         | c2          | 100.2       | 2013       | china      |
    | s3         | c3          | 100.3       | 2013       | china      |
    | null       | c5          | NULL        | 2014       | shanghai   |
    | s6         | c6          | 100.4       | 2014       | shanghai   |
    | s7         | c7          | 100.5       | 2014       | shanghai   |
    +------------+-------------+-------------+------------+------------+
  • 示例5:克隆Transaction Table2.0表

    --克隆Transaction Table2.0非分区表
    clone table mf_tt3 to new_table;
    
    --克隆Transaction Table2.0分区表
    clone table mf_tt2 partition (dd='01', hh='01') to new_table;
    说明

    目前Transaction Table2.0处于邀测阶段,默认不支持直接使用,如需您需要使用此功能,请点击申请开通,在新功能试用申请页面申请开通使用Transaction Table2.0功能。详情请参见Transaction Table2.0概述

最佳实践

实现同Region的MaxCompute项目数据迁移请参见使用CLONE TABLE实现同地域MaxCompute跨项目数据迁移

  • 本页导读 (1)
文档反馈