前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[1020]hive中分区表的创建

[1020]hive中分区表的创建

作者头像
周小董
发布2021-07-14 14:11:20
1.8K0
发布2021-07-14 14:11:20
举报
文章被收录于专栏:python前行者

1、开启分区

代码语言:javascript
复制
set hive.exec.dynamic.partition=true; 
 
set hive.exec.dynamic.partition.mode=nonstrict;

否则会出抛出异常:

2、创建分区表

创建静态分区表:

代码语言:javascript
复制
create table test_part_table(
word string,
num bigint 
)partitioned by(dt string)
row format delimited fields terminated by '\t';
 
--添加分区
alter table test_part_table add if not exists partition(dt='20190808') location '20190808';
 
--插入数据
load data local INPATH '/home/dongwentao15/dataTest/test1' overwrite into table test_part_table PARTITION (dt='20190811');
 
 
hive (dongwentao15)> select * from test_part_table;
OK
dwt     22      20190811
cn      3       20190811
un      1       20190811
fk      5       20190811
pl      19      20190811
6       NULL    20190811

第二步骤的添加分区可以省略,可以直接load数据到分区表中,在load数据的过程中,hive会自动创建分区目录。

创建动态分区表:

代码语言:javascript
复制
create table orders_part(
order_id string,
user_id string,
eval_set string,
order_number string,
order_hour_of_day string,
days_since_prior_order string
)partitioned by(order_dow string)
row format delimited fields terminated by ',';
 
--添加数据
insert into table orders_part partition (order_dow) select order_id,user_id,eval_set,order_number,order_hour_of_day,days_since_prior_order,order_dow from orders;
 
其中orders表中的字段是:
order_id,user_id,eval_set,order_number,order_dow,order_hour_of_day,days_since_prior_order

需要注意的是:动态添加分区的时候,查询的分区字段必须放在最后面(order_dow),否则结果不是你想要的;

insert…select 往表中导入数据时,查询的字段个数必须和目标的字段个数相同,不能多,也不能少,否则会报错。但是如果字段的类型不一致的话,则会使用null值填充,不会报错。而使用load data形式往hive表中装载数据时,则不会检查。如果字段多了则会丢弃,少了则会null值填充。同样如果字段类型不一致,也是使用null值填充。

来源:https://blog.csdn.net/dwt1415403329/article/details/99204552

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/07/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档