Exchange Partition
Exchange Partition
EXCHANGE PARTITION 명령은 파티션을 소스 테이블에서 대상 테이블로 옮기고 각 테이블의 메타데이터를 변경해요. 이 기능은 HIVE-4095로 구현되었고, 여러 파티션 교환은 HIVE-11745로 Hive 1.2.2, 1.3.0, 2.0.0+ 버전에서 지원됩니다.
출처: 문서
본문
명령이 실행되면 소스 테이블의 HDFS 파티션 폴더가 대상 테이블의 파티션 폴더로 이동하도록 이름이 바뀌어요. Hive metastore는 소스/대상 테이블의 메타데이터를 그에 맞게 갱신합니다.
파티션 지정은 전체 또는 일부(partial)로 할 수 있어요.
Exchange Partition 기능에 대한 자세한 내용은 Language Manual DDL을 참고하세요.
제약 조건(Constraints)
- 대상 테이블이 교환할 파티션을 이미 포함하고 있으면 안 됩니다.
- 인덱스(index)가 존재하면 작업이 실패해요.
- 트랜잭션 테이블은 소스든 대상이든 Exchange partition을 허용하지 않아요. 대신
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>
기본 사용 예시(Example Usage – Basic)
--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;
부분 파티션 지정 예시(Example Usage – Partial Partition Spec, 여러 파티션 교환)
--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;
참고로 새로 생성된 파티션 T2(ds=1)에는 T1의 스키마가 사용돼요. T1의 모든 파티션이 생성되거나 전체 작업이 실패합니다. T1의 파티션은 모두 삭제됩니다.
여러 파티션 컬럼 지정 예시(Example Usage – Partition Spec With Multiple Partition Columns)
-- 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;
더 알아보기 (Learn more)
Exchange Partition은 데이터를 실제로 복사하지 않고 폴더 이동만으로 빠르게 파티션을 옮겨요. 트랜잭션 테이블은 지원하지 않으므로 그 경우엔 LOAD DATA나 INSERT OVERWRITE를 사용하세요.