Exchange Partition
EXCHANGE PARTITION 命令会将分区从源表移动到目标表,并更改每个表的元数据。 Exchange 分区功能是HIVE-4095的一部分。 Hive 版本 1.2.2、1.3.0 和 2.0.0 作为HIVE-11745的一部分,支持交换多个分区。
执行该命令时,HDFS 中源表的分区文件夹将被重命名以将其移动到目标表的分区文件夹。 Hive 元存储将被更新,以相应地更改源表和目标表的元数据。
分区规范可以是完全或partially specified。
有关 Exchange 分区功能的其他信息,请参见语言手册 DDL。
Constraints
-
目标表不能包含要交换的分区。
-
存在索引时,该操作将失败。
-
事务表不允许作为源或目标使用 Exchange 分区。或者,使用LOAD DATA或INSERT OVERWRITE命令在事务表之间移动分区。
-
此命令要求源表名称和目标表名称都具有相同的表架构。
如果架构不同,则会引发以下异常:
The tables have different schemas. Their partitions cannot be exchanged
Syntax
ALTER TABLE <dest_table> EXCHANGE PARTITION (<[partial] partition spec>) WITH TABLE <src_table>
示例用法-基本
--Create two tables, partitioned by ds
CREATE TABLE T1(a string, b string) PARTITIONED BY (ds string);
CREATE TABLE T2(a string, b string) PARTITIONED BY (ds string);
ALTER TABLE T1 ADD PARTITION (ds='1');
--Move partition from T1 to T2
ALTER TABLE T2 EXCHANGE PARTITION (ds='1') WITH TABLE T1;
用法示例–部分分区规范(交换多个分区)
--Create two tables with multiple partition columns.
CREATE TABLE T1 (a string, b string) PARTITIONED BY (ds string, hr string);
CREATE TABLE T2 (a string, b string) PARTITIONED BY (ds string, hr string);
ALTER TABLE T1 ADD PARTITION (ds = '1', hr = '00');
ALTER TABLE T1 ADD PARTITION (ds = '1', hr = '01');
ALTER TABLE T1 ADD PARTITION (ds = '1', hr = '03');
--Alter the table, moving all the three partitions data where ds='1' from table T1 to table T2 (ds=1)
ALTER TABLE T2 EXCHANGE PARTITION (ds='1') WITH TABLE T1;
请注意,T1 的架构正在用于新创建的分区 T2(ds = 1)。将创建 T1 的所有分区,或者整个操作将失败。 T1 的所有分区均已删除.
示例用法–具有多个分区列的分区规范
-- Create two tables with multiple partition columns.
CREATE TABLE T1 (a int) PARTITIONED BY (d1 int, d2 int);
CREATE TABLE T2 (a int) PARTITIONED BY (d1 int, d2 int);
ALTER TABLE T1 ADD PARTITION (d1=1, d2=2);
-- Alter the table, moving partition data d1=1, d2=2 from table T1 to table T2
ALTER TABLE T2 EXCHANGE PARTITION (d1 = 1, d2 = 2) WITH TABLE T1;