hive中分区表的创建

hive中分区表的创建

神罗天征-长门
1>开启分区

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

否则会出抛出异常:

2>创建分区表

创建静态分区表:

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会自动创建分区目录。
创建动态分区表:

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值填充。
————————————————
版权声明:本文为CSDN博主「神罗天征-长门」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dwt1415403329/article/details/99204552

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值