HiveSQL分区的作用及创建分区表案例演示(图解)

本文详细介绍了HiveSQL中单级和多级分区表的概念、创建过程、作用以及如何通过分区实现数据组织、性能优化和管理。包括静态和动态分区的添加方法,以及如何通过年月等时间维度进行多级分区操作。
摘要由CSDN通过智能技术生成

目录

一、分区的作用

二、单级分区表

1.准备工作

2.创建数据表

3.查询数据

4.创建分区数据表

5.添加数据

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

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

三、多级分区表

1.准备工作

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

 3.查询数据

4.修改分区

 5.删除分区


一、分区的作用

           HiveSQL分区的作用是将数据划分为更小的部分,以及根据特定的字段值将数据进行组织和管理。分区的原理是通过在数据存储和查询过程中利用分区信息来提高性能和查询效率,避免全表扫描,通俗来讲分区相当于分文件夹。

具体来说,HiveSQL分区的作用包括以下几个方面:

1. 数据组织和管理:分区可以将数据按照特定的字段值进行组织和管理。通过将数据划分为更小的分区,可以更方便地进行数据的存储、查询和维护。

2. 查询性能优化:分区可以根据查询的条件过滤掉不满足条件的分区,从而减少需要扫描的数据量,提高查询的性能和效率。尤其是对于大规模数据集的查询,在使用分区时可以显著减少查询的时间。

3. 并行处理能力提升:分区可以将数据划分为更小的单元,从而提供更好的并行处理能力。在查询时,可以同时处理不同的分区,从而提高查询的并发性和整体的查询性能。

4. 存储空间优化:对于分区表,可以根据不同分区的特点选择不同的存储策略。例如,对于不经常查询的历史数据可以选择压缩存储,从而减少存储空间的占用。

5. 数据分析和统计:通过分区,可以更方便地进行数据分析和统计工作。例如,可以根据不同分区的数据分布情况,对不同分区的数据进行分析和处理,从而得到更精确的结果。

       总的来说,HiveSQL分区的作用是提供更好的数据组织、查询性能优化、并行处理能力、存储空间优化以及数据分析和统计的功能,从而更高效地管理和查询大规模数据集。


二、单级分区表

1.准备工作
-- 创建数据库
create database if not exists game;

-- 切库
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路径下

3.查询数据
select * from t_all_hero;

 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中已已经根据我们的要求分好区

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';  

三、多级分区表

    我们已经了解了单级分区但实际开发中在数据量比较大的情况下大多数采用多级分区来存储数据, 多级分区一般用采用时间来分区, 可以是: 年, 月, 日...。分区层级不建议超过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年两个区,并且也分了二级月分区

4.修改分区

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

-- 2. 修改分区.  例如: 把2023年1月修改为 2023年5月
alter table products partition(year=2023, month=1) 
rename to partition(year=2023, month=5);
 5.删除分区
-- 3. 删除分区.
alter table products drop partition(year=2023, month=3);    -- 删除2023年3月 这个分区
alter table products drop partition(month=1);               -- 删除所有的1月 这个分区
alter table products drop partition(year=2023);             -- 删除2023年 及其所有的子分区.

本篇分享已结束

  • 31
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
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 ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值