使用一个插入语句

时间:2018-02-21 08:38:31

标签: hadoop hive hiveql hive-partitions

我有表A和表B,其中B是使用名为X的字段的A的分区表。

当我想从A到B插入数据时,我通常会执行以下语句:

INSERT INTO TABLE B PARTITION(X=x) SELECT <columnsFromA> FROM A WHERE X=x

现在我想要实现的是能够插入一系列X,让我们说x1,x2,x3 ......我怎样才能在一个语句中实现这一点?

1 个答案:

答案 0 :(得分:2)

使用动态分区加载:

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

INSERT OVERWRITE TABLE table_B PARTITION(X)
select 
col_1,
col_2,
...
col_N,
X --partition column is the last one
 from 
      table_A
where X in ('x1', 'x2', 'x3'); --filter here

如果A和B中的列顺序相同,则使用select * from table_A。分区列(X)应该是最后一个。