hive之建立分区表和分区

1. 建立分区表 create table

单分区表:其中分区字段是partdate,注意分区字段不能和表字段一样,否则会报重复的错

create table test_t2(words string,frequency string) partitioned by (partdate string) row format delimited fields terminated by '\1';

多分区表:id在前,address在后,注意多个分区字段时,是有先后顺序的

create table test_table_hive(name string,school string) partitioned by (id int,address string) row format delimited fields terminated by '\1';

2. 新建分区:建立分区表之后,此时没有数据,也没有分区,需要建立分区

查看分区命令show partitions:

show partitions test_table_hive;

建立单分区 alter table:

alter table test_t2 add partition(partdate='20191030');
alter table test_t2 add partition(partdate=20191030); #也可,可能是内部转换了格式
alter table test_t2 add IF NOT EXISTS partition (partdate='20191031'); #避免重复建立分区出错

alter table test_t2 add IF NOT EXISTS partition (partdate='20191031') partition (partdate='20191101'); #一次建立多个分区

建立多字段分区:

alter table test_table_hive add partition(id=1,adress='chengdu');
#显示分区
show partitions test_table_hive;

 

 一次建立多个多字段分区,注意多字段时,必须填满这多个分区字段,例如此时就不能只用 id 来分区:

alter table test_table_hive add partition(id=2,adress='beijing') partition(id=3,adress='shanghai');
show partitions test_table_hive;

 

 

3. 删除分区 drop

alter table test_t2 drop partition(partdate='20191030');

附加:显示当前数据库

select current_database();

 

参考:

https://blog.csdn.net/qq_36743482/article/details/78418343

posted on 2019-11-17 21:36  落日峡谷  阅读(28865)  评论(0编辑  收藏  举报

导航