* insert overwrite,删除在city\_code=‘200’,cur\_day='20231111’分区下数据,重新写入数据。
insert overwrite table test_1
partition(city_code=‘200’,cur_day=‘20231111’)
select
t.id,
t.name,
t.classes,
t.scores
from test_1_tmp t where city_code=‘200’
;
### 动态分区插入数据
* insert into / insert overwrite
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
insert into table test_1
partition(city_code,cur_day)
select
t.id,
t.name,
t.classes,
t.scores,
t.city_code,
t.cur_day
from test_1_tmp t
;
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table test_1
partition(city_code,cur_day)
select
t.id,
t.name,
t.classes,
t.scores,
t.city_code,
t.cur_day
from test_1_tmp t
;
>
> insert into table 插入动态分区,如果被插入表原来就有该分区,则追加插入数据;否则就正常插入数据。
> insert overwrite table 插入动态分区,如果被插入表原来就有该分区,删除原来该分区下数据,重新插入;否则就正常插入数据。
>
>
>
**举例:**
未将test\_2\_tmp数据插入到test\_1时,test\_1表的数据情况:
select * from test_1;
![在这里插入图片描述]()
表test\_2\_tmp的情况:
desc test_2_tmp;
±-----------±-----------±---------+
| col_name | data_type | comment |
±-----------±-----------±---------+
| id | string | |
| name | string | |
| classes | string | |
| scores | int | |
| city_code | string | |
| cur_day | string | |
±-----------±-----------±---------+
select * from test_2_tmp;
![在这里插入图片描述]()
用insert overwrite table重写写入(city\_code='200’与cur\_day='20231111’分区)和(city\_code='763’与cur\_day='20231112’分区),如下:
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table test_1
partition(city_code,cur_day)
select
t.id,
t.name,
t.classes,
t.scores,
t.city_code,
t.cur_day
from test_2_tmp t
;select * from test_1;
![在这里插入图片描述]()
>
> **注意:**
>
>
>
>
> 1、hive支持全动态分区,但是在使用前必须设置以下参数:
> set hive.exec.dynamic.partition=true;
> set hive.exec.dynamic.partition.mode=nonstrict;
> 2、字段和目标表动态分区的对应关系是由字段顺序决定,并不是由列名称决定的。
>
>
>
>
> 3、向动态分区插入数据时,动态分区必须在select列表中,否则会执行失败。
>
>
>
insert into table test_1
partition(city_code,cur_day)
select
t.id,
t.name,
t.classes,
t.scores
from test_1_tmp t
;
报错:
Error: Error while compiling statement: FAILED: SemanticException [Error 10044]: Line 1:18 Cannot insert into target table because column number/types are different ‘cur_day’: Table insclause-0 has 6 columns, but query has 4 columns. (state=42000,code=10044)
### 动静混合分区插入数据
* insert into / insert overwrite
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=strict;
insert into table test_1
partition(city_code=‘763’,cur_day)
select
t.id,
t.name,
t.classes,
t.scores,
t.cur_day
from test_1_tmp t
;
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=strict;
insert overwrite table test_1
partition(city_code=‘763’,cur_day)
select
t.id,
t.name,
t.classes,
t.scores,
t.cur_day
from test_1_tmp t
;
>
> insert into table 插入动静混合分区,如果被插入表原来就有该分区,则追加插入数据;否则就正常插入数据。
> insert overwrite table 插入动静混合分区,如果被插入表原来就有该分区,删除原来该分区下数据,重新插入;否则就正常插入数据。
>
>
>
**举例:**
未将test\_2\_tmp数据插入到test\_1时,test\_1表的数据情况:
select * from test_1;
![在这里插入图片描述]()
表test\_2\_tmp的情况:
select * from test_2_tmp;
![在这里插入图片描述]()
用insert into追加插入(city\_code=‘763’,cur\_day)分区的情况,如下:
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=strict;
insert into table test_1
partition(city_code=‘763’,cur_day)