将两个分区的 table 合并为一个 table 但合并为两个不同的分区

Combine two partitioned tables into one table but into two different partitions

我是 Hive 的新手,如果有人可以帮助我处理我正在处理的 Hive 查询,我将不胜感激。

有两个 tableA 和 B,架构完全相同,但数据不同,有 4 个分区。我需要将这两个 table 组合成一个 table 和 (4 + 1 = 5) 个分区。添加的分区告诉 table 数据来自哪个。例如,假设新分区被命名为"source"。如果数据来自 table A,则源将等于 "from_A",如果数据来自 table B,则源将等于 "from_B"。

hive> desc A;
OK
col1 string,
col2 string,
DD   string,                                    
EE   string,                                    
FF   string,                                    
GG   string 

# Partition Information      
# col_name              data_type               

DD              string                                      
EE              string                                      
FF                  string                                      
GG              string 

hive> desc B;
OK
col1 string,
col2 string,
DD   string,                                    
EE   string,                                    
FF   string,                                    
GG   string

# Partition Information      
# col_name              data_type               comment             

DD              string                                      
EE              string                                      
FF                  string                                      
GG              string

创建新分区 table

Create table C (
col1 string,
col2 string
)
partitioned by (
source string,
DD   string,                                    
EE   string,                                    
FF   string,                                    
GG   string
);

然后加载数据到新的 table:

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

insert overwrite table C partition(source,DD,EE,FF,GG)
select col1, col2, 
       --partitions
      'from_A' source, DD, EE, FF, GG 
  from A
distribute by DD, EE, FF, GG;

并并行加载来自 table B 的数据:

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

insert overwrite table C partition(source,DD,EE,FF,GG)
select col1, col2, 
      --partitions
      'from_B' source, DD, EE, FF, GG 
 from B
distribute by DD, EE, FF, GG;