当数据量非常大的时候,我们会考虑将hive表进行分区,从而加快查询效率。
以内部分区表为例:
原始数据如下(以\t分割),我们用性别字段sex来进行分区
id age sex
1 12 boy
2 11 boy
3 10 boy
4 10 boy
5 12 girl
6 12 girl
7 11 girl
8 10 girl
1.创建内部分区表:
在创建分区表时,就要指定分区字段。
create table inner_par_people(
id int,
age int
)
partitioned by (sex string)
row format delimited fields terminated by '\t';
2.添加partition
alter table inner_par_people add if not exists partition(sex='boy') location 'boy' partition (sex='girl') location 'girl';
3.导入数据
3.1 创建中间表,将数据导入中间表中
create table inner_person_t(
id int,
age int,
sex string
)
row format delimited fields terminated by '\t';
3.2 将中间表的数据导入分区表中
导入男生数据
insert into table inner_par_people partition(sex='boy') select id,age from inner_person_t where sex='boy';
导入女生数据
insert into table inner_par_people partition(sex='girl') select id,age from inner_person_t where sex='girl';
4.查看数据
select * from inner_par_people;
在web端查看分区目录
删除分区
alter table inner_par_people drop if exists partition (sex='boy');
查看数据
select * from inner_par_people;