HiveSQL分区的作用及创建分区表案例演示(图解)_hive 动态分区sql(1)

role_assist   string comment '次要定位'

) comment ‘射手表’
row format delimited fields terminated by ‘\t’;



2.2上传(6个)源文件到该hive表的HDFS路径下


![](https://img-blog.csdnimg.cn/direct/c0eb55c8031846318238ae7d094d863f.png)


##### 3.查询数据



select * from t_all_hero;


![](https://img-blog.csdnimg.cn/direct/9211abf933aa4a01881d8b1708b554e6.png)


 3.1查询出所有的archer数据



select * from t_all_hero where role_main=‘archer’;



问:虽然我们实现了需求, 但是需要进行全表扫描, 如何精准的获取到我们想要的数据呢?



答:可以采用分区表的思路来管理, 把各个职业的数据放到不同的文件夹中即可


##### 4.创建分区数据表



– 1. 创建分区表, 指定分区字段.
create table t_all_hero_part(
id int comment ‘ID’,
name string comment ‘英雄’,
hp_max int comment ‘最大生命’,
mp_max int comment ‘最大法力’,
attack_max int comment ‘最高物攻’,
defense_max int comment ‘最大物防’,
attack_range string comment ‘攻击范围’,
role_main string comment ‘主要定位’,
role_assist string comment ‘次要定位’
) comment ‘角色表’
partitioned by (role string comment ‘角色字段-充当分区字段’) – 核心细节: 分区字段必须是表中没有的字段.
row format delimited fields terminated by ‘\t’;



注意:分区字段必须是新的字段,表中没有的字段哦


##### 5.添加数据


###### 5.1添加方式1:静态分区(需要指定分区字段和值)



load data local inpath ‘/export/hivedata/archer.txt’ into table t_all_hero_part partition(role=‘sheshou’);
load data local inpath ‘/export/hivedata/assassin.txt’ into table t_all_hero_part partition(role=‘cike’);
load data local inpath ‘/export/hivedata/mage.txt’ into table t_all_hero_part partition(role=‘fashi’);
load data local inpath ‘/export/hivedata/support.txt’ into table t_all_hero_part partition(role=‘fuzhu’);
load data local inpath ‘/export/hivedata/tank.txt’ into table t_all_hero_part partition(role=‘tanke’);
load data local inpath ‘/export/hivedata/warrior.txt’ into table t_all_hero_part partition(role=‘zhanshi’);


5.1.1此时HDFS中已已经根据我们的要求分好区


![](https://img-blog.csdnimg.cn/direct/9f4785fbffe24fa88660fc2bfe92d990.png)


5.1.2我们再次查询archer所有的数据时就可以根据分区字段进行筛选,避免全表扫描,提高查询效率.



select * from t_all_hero_part where role=‘sheshou’;


###### 5.2添加方式2:动态分区(只需指定分区字段,分区字段相同的数据自动分配到同一个区)



在进行动态分区前建议: 手动关闭严格模式



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


5.2.1创建分区表



– 1. 创建分区表.
create table t_all_hero_part_dynamic(
id int comment ‘ID’,
name string comment ‘英雄’,
hp_max int comment ‘最大生命’,
mp_max int comment ‘最大法力’,
attack_max int comment ‘最高物攻’,
defense_max int comment ‘最大物防’,
attack_range string comment ‘攻击范围’,
role_main string comment ‘主要定位’,
role_assist string comment ‘次要定位’
) comment ‘角色表’
partitioned by (role string comment ‘角色字段-充当分区字段’) – 核心细节: 分区字段必须是表中没有的字段.
row format delimited fields terminated by ‘\t’;


5.2.2动态分区方式添加数据



由于建表时增加一个role的分区字段,所以总共有9个普通字段和1个分区字段,所以插入数据时select语句中需要单独加上一个分区字段



– 2. 动态分区的方式, 添加数据.
insert into table t_all_hero_part_dynamic partition(role)
select *, role_main from t_all_hero; – role main字段做为分区字段使用


5.2.3查询分区表所有数据 



– 3. 查询分区表的数据.
select * from t_all_hero_part_dynamic;


 5.2.4查询分区表中archer所有数据



select * from t_all_hero_part_dynamic where role=‘archer’;


![](https://img-blog.csdnimg.cn/direct/711d1797c72c4325a299087e973027d6.png)


#### 三、多级分区表



*我们已经了解了单级分区但实际开发中在数据量比较大的情况下大多数采用多级分区来存储数据, 多级分区一般用采用时间来分区, 可以是: 年, 月, 日...。分区层级不建议超过3级, 一般是: 年, 月2级就够了。*

##### 1.准备工作



– 创建数据库
create database if not exists products;
– 切库
use products;


##### 2.创建分区表(按照年、月分区)



– 1. 创建商品表, 按照: 年, 月分区.
create table products(
pid int,
pname string,
price int,
cid string
) comment ‘商品表’
partitioned by (year int, month int) – 按照年, 月分区, 2级分区
row format delimited fields terminated by ‘,’;


#####  3.查询数据


3.1查找2023年1月分区下的所有数据



– 查找2023年1月分区下的所有数据
select * from products where year=2023 and month=1;


 3.1.2HDFS中已经按要求分为2023年和2024年两个区,并且也分了二级月分区


![](https://img-blog.csdnimg.cn/direct/96a0e67946044846a33eb74e5f824681.png)![](https://img-blog.csdnimg.cn/direct/2e54e64c2353422cb79d4bf009a92e76.png)


##### 4.修改分区


4.1把2023年1月修改为 2023年5月



![img](https://img-blog.csdnimg.cn/img_convert/ce516423640f9a84bdcf84803d91985f.png)
![img](https://img-blog.csdnimg.cn/img_convert/d6ed1ab42b52bd2efb98a4864563211a.png)
![img](https://img-blog.csdnimg.cn/img_convert/09fbcb73776a796e80f5927ae4093b66.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

g-9sa7KxsE-1714408849193)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值