全部產品
Search
文件中心

MaxCompute:CLONE TABLE

更新時間:Dec 10, 2025

CLONE TABLE支援高效地將源表資料複製到目標表中,適用於表資料移轉情境。本文以具體樣本介紹如何使用CLONE TABLE功能。

適用範圍

  • 目標表限制

    • 目標表需相容源表的Schema。

    • 目標表已存在時,單次最多可複製10,000個分區。

    • 目標表不存在時,無分區數量限制,滿足原子性。

  • 外部表格限制:不支援對外部表格使用CLONE TABLE命令複製表資料。

  • 支援分區表和非分區表,支援對聚簇表使用CLONE TABLE命令複製表資料。

  • 跨地區限制:不支援在跨地區的MaxCompute專案之間使用CLONE TABLE命令複製表資料。

  • 跨儲存類型限制

    不支援在跨不同儲存類型的專案之間使用CLONE TABLE命令複製表資料,即多AZ儲存類型專案的表無法通過CLONE TABLE命令複製到單AZ儲存類型專案中,反之亦然。

  • 跨不同儲存類型的專案之間複製表推薦方案:

    • 非分區表採用CREATE TABLE AS方式遷移資料。

    • 分區表通過CREATE TABLE LIKE先建表,然後執行INSERT OVERWRITE遷移資料。

命令格式

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中的資料,命令樣本如下:

    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的資訊,驗證資料準確性。
    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   |
    +------------+-------------+-------------+------------+------------+