Hive分区表(重点)

1.分区表:

实际上就是对应一个HDFS文件系统个上的独立的文件夹,该文件夹下是该分区所有的数据文件.Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集.在查询的时候时,根据where条件找到分目录下获取数据,避免做了全局扫描,提高了查询的效率.
注意:实际上,分区也是表的一个字段,且该字段往往放在所有字段的最后,所以后面做项目或者突然多添加了一行数据的时候,都能正常理解。

2.基本操作演示

(1)创建分区表的语法

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

(2)加载数据到分区表中

load data local inpath '/opt/module/datas/dept1.txt' 
into table dept_partition 
partition (no='2021-05-07');

load data local inpath '/opt/module/datas/dept2.txt' 
into table dept_partition 
partition (no='2021-05-08');

load data local inpath '/opt/module/datas/dept3.txt' 
into table dept_partition 
partition (no='2021-05-09');

进如到dept_partition数据库查看表结构:
在这里插入图片描述
在这里插入图片描述

生成了分区表目录:no=2021-05-07,no=2021-05-08,no=2021-05-09三个独立目录,每个目录里面又有相对应的文件。

(3)查询分区表中的数据:

单分区查询

select * from dept_partition where no='2021-05-07';

多分区查询

select * from dept_partition where no='2021-05-07'
union
select * from dept_partition where no='2021-05-08'
union
select * from dept_partition where no='2021-05-09';

使用union多分区查询的时候会走mapreduce。
如果是查询所有的分区,直接采用select * from dept_partition;不走mapreduce速度更快。

(4)增加分区

增加单个分区:

alter table dept_partition add partition(no='2021-05-10');

增加多个分区:

alter table dept_partition add 
partition(no='2021-05-11') 
partition(no='2021-05-12');

下图为增加的新分区:
在这里插入图片描述

(5)删除分区

删除单个分区:

alter table dept_partition drop partition(no='2021-05-10');

删除多个分区:

alter table dept_partition drop
partition(no='2021-05-11'), 
partition(no='2021-05-12');

注意:在这里,drop后面的多个分区,之间要采用逗号来隔开。而增加多个分区的时候中间采用空格即可。

二级分区

创建二级分区表:

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

表的结构如下:
在这里插入图片描述
加载数据到二级目录下:

load data local inpath '/opt/module/datas/dept1.txt' 
into table dept_partition2
partition(month='2021-05',day='07');

查询分区下的数据:

select * from dept_partition2 where month='2021-05' and day ='07';
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦里Coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值