THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
The Exchange Partition EXCHANGE PARTITION command will move a partition from a source table to target table and alter each table's metadata. The Exchange Partition feature is implemented as part of
.
Exchanging multiple partitions is supported in Hive versions 1.2.2. 1.3.0, 2.0.0+ as part of
Jira | ||||||
---|---|---|---|---|---|---|
|
Exchanging multiple partitions is supported in Hive versions 1.2.2. 1.3.0, 2.0.0+ as part of
Jira | ||||||
---|---|---|---|---|---|---|
|
...
When the command is executed, the source table's partition folder in HDFS will be renamed to move it to the destination table's partition folder. The Hive metastore will be updated to change the metadata of the source and destination tables accordingly.
The partition spec can be fully or partially specified.
Constraints
- The destination table cannot contain the partition to be exchanged.
- The operation fails in the presence of an index.
- This command requires both the source and destination table names to have the same table schema.
If the schemas are different, the following exception is thrown:
The tables have different schemas. Their partitions cannot be exchanged
Syntax
Code Block |
---|
alterALTER tableTABLE <tablename><source_table> exchangeEXCHANGE partitionPARTITION (<(partial)?partition spec>) withWITH tableTABLE <tablename><dest_table> |
Example usage - Basic
Code Block |
---|
--Create two tables, one partitioned by ds, one not partitioned createCREATE tableTABLE T1(a string, b string) partitionedPARTITIONED byBY (ds string); createCREATE tableTABLE T2(a string, b string); --Exchange partition data from T2T1 to T1T2(ds=1) alterALTER tableTABLE T1 exchangeEXCHANGE partitionPARTITION (ds='1') withWITH tableTABLE T2 |
Note that the schema for T2 T1 is being used for the newly created partition T1T2(ds=1).
Example usage - Rolling Hourly to Daily PartitionsPartial partition spec
Code Block |
---|
--Create two tables, one with multiple partitions, one with a single partition. createCREATE tableTABLE T1(a string, b string) partitionedPARTITIONED byBY (ds string, hr string); createCREATE tableTABLE T2(a string, b string) partitionedPARTITIONED byBY (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 partition data where ds='1' from table T2T1 to table T1T2 (ds=1) alterALTER tableTABLE T1 exchangeEXCHANGE partitionPARTITION (ds='1') withWITH tableTABLE T2 |
Note that the schema for T2 T1 is being used for the newly created partition T1T2(ds=1). Either all the partitions of T1 will get created or the whole operation will fail. All partitions of T2 T1 are dropped.
Example Usage - Exchanging Multiple partitions
Code Block | ||
---|---|---|
| ||
-- Create two tables, both with multiple partitions CREATE TABLE t3t1 (a int) PARTITIONED BY (d1 int, d2 int); CREATE TABLE t4t2 (a int) PARTITIONED BY (d1 int, d2 int); -- Alter the table, moving partition data where d1=1, d2=2 from table T4T1 to table T3T2 ALTER TABLE t4t1 EXCHANGE PARTITION (d1 = 1, d2 = 1) WITH TABLE t3t2; |