前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hive静态分区、动态分区、多重分区全解析

Hive静态分区、动态分区、多重分区全解析

作者头像
大数据学习与分享
发布2023-02-26 10:48:20
1.9K0
发布2023-02-26 10:48:20
举报

01

分区表的引入、产生背景

现有6份数据文件,分别记录了《王者荣耀》中6种位置的英雄相关信息。现要求通过建立一张表t_all_hero,把6份文件同时映射加载。

代码语言:javascript
复制
create table t_all_hero(    id int,    name string,    hp_max int,    mp_max int,    attack_max int,    defense_max int,    attack_range string,    role_main string,    role_assist string)row format delimitedfields terminated by "\t";

加载数据文件到HDFS指定路径下:

现要求查询role_main主要定位是射手并且hp_max最大生命大于6000的有几个,sql语句如下:

代码语言:javascript
复制
select count(*) from t_all_hero where role_main="archer" and hp_max >6000;

思考一下:where语句的背后需要进行全表扫描才能过滤出结果,对于hive来说需要扫描表下面的每一个文件。如果数据文件特别多的话,效率很慢也没必要。本需求中,只需要扫描archer.txt文件即可,如何优化可以加快查询,减少全表扫描呢?

02

分区表的概念、创建

当Hive表对应的数据量大、文件多时,为了避免查询时全表扫描数据,Hive支持根据用户指定的字段进行分区,分区的字段可以是日期、地域、种类等具有标识意义的字段。比如把一整年的数据根据月份划分12个月(12个分区),后续就可以查询指定月份分区的数据,尽可能避免了全表扫描查询。

分区表建表语法:

代码语言:javascript
复制
CREATE TABLE table_name (column1 data_type, column2 data_type) PARTITIONED BY (partition1 data_type, partition2 data_type,….);

针对《王者荣耀》英雄数据,重新创建一张分区表t_all_hero_part,以role角色作为分区字段。

代码语言:javascript
复制
create table t_all_hero_part(       id int,       name string,       hp_max int,       mp_max int,       attack_max int,       defense_max int,       attack_range string,       role_main string,       role_assist string) partitioned by (role string)row format delimitedfields terminated by "\t";

需要注意:分区字段不能是表中已经存在的字段,因为分区字段最终也会以虚拟字段的形式显示在表结构上。

03

分区表数据加载--静态分区

所谓静态分区指的是分区的字段值是由用户在加载数据的时候手动指定的。

语法如下:

代码语言:javascript
复制
load data [local] inpath ' ' into table tablename partition(分区字段='分区值'...);

Local表示数据是位于本地文件系统还是HDFS文件系统。关于load语句后续详细展开讲解。

静态加载数据操作如下,文件都位于Hive服务器所在机器本地文件系统上。

代码语言:javascript
复制
load data local inpath '/root/hivedata/archer.txt' into table t_all_hero_part partition(role='sheshou');load data local inpath '/root/hivedata/assassin.txt' into table t_all_hero_part partition(role='cike');load data local inpath '/root/hivedata/mage.txt' into table t_all_hero_part partition(role='fashi');load data local inpath '/root/hivedata/support.txt' into table t_all_hero_part partition(role='fuzhu');load data local inpath '/root/hivedata/tank.txt' into table t_all_hero_part partition(role='tanke');load data local inpath '/root/hivedata/warrior.txt' into table t_all_hero_part partition(role='zhanshi');

04

分区表数据加载--动态分区

往hive分区表中插入加载数据时,如果需要创建的分区很多,则需要复制粘贴修改很多sql去执行,效率低。因为hive是批处理系统,所以hive提供了一个动态分区功能,其可以基于查询参数的位置去推断分区的名称,从而建立分区。

所谓动态分区指的是分区的字段值是基于查询结果自动推断出来的。核心语法就是insert+select。

启用hive动态分区,需要在hive会话中设置两个参数:

set hive.exec.dynamic.partition=true;

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

第一个参数表示开启动态分区功能,第二个参数指定动态分区的模式。分为nonstick非严格模式和strict严格模式。strict严格模式要求至少有一个分区为静态分区。

创建一张新的分区表t_all_hero_part_dynamic

代码语言:javascript
复制
create table t_all_hero_part_dynamic(         id int,         name string,         hp_max int,         mp_max int,         attack_max int,         defense_max int,         attack_range string,         role_main string,         role_assist string) partitioned by (role string)row format delimitedfields terminated by "\t";

执行动态分区插入

代码语言:javascript
复制
insert into table t_all_hero_part_dynamic partition(role) select tmp.*,tmp.role_main from t_all_hero tmp;

动态分区插入时,分区值是根据查询返回字段位置自动推断的。

05

分区表的本质

外表上看起来分区表好像没多大变化,只不过多了一个分区字段。实际上在底层管理数据的方式发生了改变。这里直接去HDFS查看区别。

非分区表:t_all_hero

分区表:t_all_hero_part

分区的概念提供了一种将Hive表数据分离为多个文件/目录的方法。不同分区对应着不同的文件夹,同一分区的数据存储在同一个文件夹下。只需要根据分区值找到对应的文件夹,扫描本分区下的文件即可,避免全表数据扫描。

06

分区表的使用

分区表的使用重点在于:

一、建表时根据业务场景设置合适的分区字段。比如日期、地域、类别等;

二、查询的时候尽量先使用where进行分区过滤,查询指定分区的数据,避免全表扫描。

比如:查询英雄主要定位是射手并且最大生命大于6000的个数。使用分区表查询和使用非分区表进行查询,SQL如下:

代码语言:javascript
复制
--非分区表 全表扫描过滤查询select count(*) from t_all_hero where role_main="archer" and hp_max >6000;--分区表 先基于分区过滤 再查询select count(*) from t_all_hero_part where role="sheshou" and hp_max >6000;

想一想:底层执行性能来说,分区表的优势在哪里?

07

分区表的注意事项

一、分区表不是建表的必要语法规则,是一种优化手段表,可选;

二、分区字段不能是表中已有的字段,不能重复;

三、分区字段是虚拟字段,其数据并不存储在底层的文件中;

四、分区字段值的确定来自于用户价值数据手动指定(静态分区)或者根据查询结果位置自动推断(动态分区

五、Hive支持多重分区,也就是说在分区的基础上继续分区,划分更加细粒度

08

多重分区表

通过建表语句中关于分区的相关语法可以发现,Hive支持多个分区字段:PARTITIONED BY (partition1 data_type, partition2 data_type,….)。

多重分区下,分区之间是一种递进关系,可以理解为在前一个分区的基础上继续分区。从HDFS的角度来看就是文件夹下继续划分子文件夹。比如:把全国人口数据首先根据省进行分区,然后根据市进行划分,如果你需要甚至可以继续根据区县再划分,此时就是3分区表。

代码语言:javascript
复制
--单分区表,按省份分区create table t_user_province (id int, name string,age int) partitioned by (province string);--双分区表,按省份和市分区create table t_user_province_city (id int, name string,age int) partitioned by (province string, city string);--三分区表,按省份、市、县分区create table t_user_province_city_county (id int, name string,age int) partitioned by (province string, city string,county string);

多分区表的数据插入和查询使用


免责声明:本公众号所发布的文章为本公众号原创,或者是在网络搜索到的优秀文章进行的编辑整理,文章版权归原作者所有,仅供读者朋友们学习、参考。对于分享的非原创文章,有些因为无法找到真正来源,如果标错来源或者对于文章中所使用的图片、连接等所包含但不限于软件、资料等,如有侵权,请直接联系后台,说明具体的文章,后台会尽快删除。给您带来的不便,深表歉意。


本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-02-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大数据学习与分享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
大数据
全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档