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

img
img
img

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

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

需要这份系统化资料的朋友,可以戳这里获取

use game;


##### 2.创建数据表



2.1首先我们先创建一张未分区的数据表,以便后续与分区数据表做对比.



– 1. 建表.
create table t_all_hero(
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 ‘射手表’
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 ‘,’;




![img](https://img-blog.csdnimg.cn/img_convert/8c4b20df70d4f6307eeebcb84d917a2e.png)
![img](https://img-blog.csdnimg.cn/img_convert/5281016eca0c5c3be9e879548f9c1c53.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

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


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

74719644)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

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


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 11
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive SQL中的分区是指根据特定的字段值将数据存储在不同的文件夹或子文件夹中的分区可以提高查询性能和数据管理的效率。分区可以分为静态分区动态分区两种类型。在创建分区时,可以通过指定分区字段来实现数据的分区存储。分区字段不能是已有的字段,也不能重复。分区字段的值可以手动指定(静态分区)或根据查询结果位置自动推断(动态分区)。 动态分区是一种根据查询结果自动推断分区字段值的方式。在Hive中启用动态分区需要设置两个参数,即hive.exec.dynamic.partition为true和hive.exec.dynamic.partition.mode为nonstrict。动态分区可以使用insert select语法来实现,通过该语法可以根据查询结果动态地将数据插入分区中。 Hive还支持多重分区,即在分区的基础上继续进行分区。多重分区可以通过指定多个分区字段来实现,不同分区字段之间具有递进关系,可以理解为在前一个分区的基础上再进行分区,划分更加细的粒度。从HDFS的角度来看,多重分区就是在文件夹下继续划分子文件夹。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [HiveSQL 分区](https://blog.csdn.net/weixin_53570636/article/details/127240576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值