暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

hive分区表之动静结合

跟星宸玩大数据 2021-08-05
532
结论

hive分区表中导入数据需要先静后动

小伙伴们可能会比较疑惑,这句话是什么意思呢?不要着急,接下来我会结合具体的案例来解释什么是先静后动

首先是测试数据(test.dat)

01,kolton,2020,01,02
02,sam,2020,02,01
03,bob,2020,04,12
04,cassel,2021,05,26
05,mark,2021,03,10
06,gary,2021,07,20
07,james,2021,03,15
08,bill,2020,02,10

有了测试数据后,就可以着手创建hive表test_source并加载数据了,这是源表,我们会通过这张表向分区表中添加数据。

create table test_source(
id string,
name string,
year string,
month string,
day string
)
row format delimited
fields terminated by ',';


load data local inpath "/root/data/test.dat" into table test_source; --记得更改自己的数据目录哦

创建目标表,也就是真正的测试表

create table test(
id string,
name string)
partitioned by (year string, month string, day string)
row format delimited
fields terminated by ',';

这张表中有三个分区分别是year,month,day

测试方法:

第一次:year为静态分区,month为动态分区,day为动态分区---------->数据插入成功。

insert overwrite table test partition(year=2020,month,day)
select id, name, month, day from test_source where year = 2020;

第二次:year为静态分区,month为动态分区,day为静态分区---------->数据插入失败。

insert overwrite table test partition(year=2020,month,day=12)
select id, name, month, day from test_source where year = 2020 and day = 12;


FAILED: SemanticException [Error 10094]: Line 1:38 Dynamic partition cannot be the parent of a static partition '12'

第三次:year为动态分区,month为静态分区,day为动态分区---------->数据插入失败

insert overwrite table test partition(year,month=02,day)
select idnamemonthday from test_source where month = 02;


FAILED: SemanticException [Error 10094]: Line 1:38 Dynamic partition cannot be the parent of a static partition 'day'

由此可以看出,hive分区表使用混合分区时必须是先静后动。


更多内容

- 扫码关注 -
微信号| xcbigdata
知乎账号| 跟星宸玩大数据
文章转载自跟星宸玩大数据,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论