Hive的分区表

Hive分区表
Hive分区表对应的是HDFS上独立的文件夹,该文件夹存放的是该分区的所有数据,其实分区就是分目录。Hive通过分区把一个大的数据集根据业务需要分割成小的数据集,在查询时能够通过where关键词选择指定分区,从而提高查找效率。为什么这样能够提高查找效率呢?其实是因为Hive存放的数据是没有索引的,如果没有建立分区直接查询,Hive就会暴力查询,效率很低,所以通过分区能很好提高Hive的查询效率。分区还能够更加方便的管理一些特殊数据,例如一些日志数据,可以是一个天一个分区或者一个月一个分区,视数据量而定,这样就能很好地管理日志数据了。

分区表的基本操作
现在有以下数据:
10 ACCOUNTING 1700
20 RESEARCH 1800
30 SALES 1900
40 OPERATIONS 1700

1.创建分区表语句,表中有三列数据,并指定按月份分区,但是数据中心并没有月份,月份是插入或者加载数据时指定的:

create table dept_partition(
deptno int, dname string, loc string
)
partitioned by (month string)
row format delimited fields terminated by '\t';

2.把数据加载到分区表里面,并指定这份数据的月份,即指定分区:

load data local inpath '/opt/module/data/dept.txt' into table default.dept_partition partition(month='201709');
'/opt/module/data/dept.txt' into table default.dept_partition partition(month='201708');
'/opt/module/data/dept.txt' into table default.dept_partition partition(month='201707');

然后就能在HDFS上即可看到,在dep_partition数据库的文件夹里有两个文件夹:
在这里插入图片描述
3.查询指定分区的数据:
单分区查询:

select * from dept_partition where month='201709';

多分区联合查询:

select * from dept_partition where month='201709'
              union
              select * from dept_partition where month='201708'
              union
              select * from dept_partition where month='201707';

查询结果:(此结果为三个分区的并集)
在这里插入图片描述
4.增加分区
增加单个分区:

alter table dept_partition add partition(month='201706');

同时增加多个分区:

alter table dept_partition add partition(month='201705') partition(month='201704');

5.删除分区
删除单个分区:

alter table dept_partition drop partition(month='201704';

同时删除多个分区:

alter table dept_partition drop partition(month='201705'),partition(month='201706');

6.查看分区表的多分区情况

show partitions dept_partition;

在这里插入图片描述
7.查看分区表结构

desc formatted dept_partition;

在这里插入图片描述

分区表注意事项
1.创建二级分区表

create table dept_partition2(deptno int,dname string,loc string)
partitioned by (month string,day string)
row format delimited fields terminated by '\t';

2.加载数据

load data local inpath '/opt/module/data/dept.txt' into table
 default.dept_partition2 partition(month='201709', day='13');

3.把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式
(1)方式一:上传数据后修复
上传数据

dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=12;
dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=12;
查询数据(查询不到刚上传的数据)
select * from dept_partition2 where month='201709' and day='12';

执行修复命令

 msck repair table dept_partition2;

再次查询数据

select * from dept_partition2 where month='201709' and day='12';
(2)方式二:上传数据后添加分区
上传数据
dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=11;
dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=11;
执行添加分区
alter table dept_partition2 add partition(month='201709',
 day='11');
查询数据
select * from dept_partition2 where month='201709' and day='11';
(3)方式三:创建文件夹后load数据到分区
	创建目录
dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=10;

上传数据

load data local inpath '/opt/module/datas/dept.txt' into table
 dept_partition2 partition(month='201709',day='10');

查询数据

select * from dept_partition2 where month='201709' and day='10';
发布了17 篇原创文章 · 获赞 11 · 访问量 5112
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章