当前位置:   article > 正文

【hive】- 使用insert into insert overwrite插入数据到静态分区、动态分区、动静态分区_hive insert into_hive insert overwrite 分区

hive insert overwrite 分区

举例:

未将test_2_tmp数据插入到test_1时,test_1表的数据情况:

select \* from test_1;

  • 1
  • 2

在这里插入图片描述

表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     |          |
+------------+------------+----------+

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
select \* from test_2_tmp;

  • 1
  • 2

在这里插入图片描述

用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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述

注意:

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
;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

报错:

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)

  • 1
  • 2

动静混合分区插入数据

  • 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
;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
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
;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

insert into table 插入动静混合分区,如果被插入表原来就有该分区,则追加插入数据;否则就正常插入数据。
insert overwrite table 插入动静混合分区,如果被插入表原来就有该分区,删除原来该分区下数据,重新插入;否则就正常插入数据。

举例:

未将test_2_tmp数据插入到test_1时,test_1表的数据情况:

select \* from test_1;

  • 1
  • 2

在这里插入图片描述

表test_2_tmp的情况:

select \* from test_2_tmp;

  • 1
  • 2

在这里插入图片描述

用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)
select
t.id,
t.name,
t.classes,
t.scores,
t.cur_day
from test_2_tmp t
;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

用insert overwrite重写(city_code=‘763’,cur_day)分区的情况,如下:

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_2_tmp t
;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

注意: 动静态分区混合插入,静态分区要在动态分区前面。

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=strict;
insert into table test_1
partition(cur_day,city_code='763')
select
t.id,
t.name,
t.classes,
t.scores,
t.cur_day
from test_1_tmp t
;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

报错:

Error: Error while compiling statement: FAILED: ValidationFailureSemanticException summary_fz_province.test_1: Partition spec {city=763, cur_day=null} contains non-partition columns (state=42000,code=40000)

  • 1
  • 2

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

52)]
[外链图片转存中…(img-NnMHaaot-1714721895452)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/853139
推荐阅读
相关标签
  

闽ICP备14008679号