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

总的来说,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路径下*

hive 写入有分区得sql hive sql创建分区表_字段

3.查询数据
select * from t_all_hero;

hive 写入有分区得sql hive sql创建分区表_大数据_02

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

hive 写入有分区得sql hive sql创建分区表_hive 写入有分区得sql_03

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

hive 写入有分区得sql hive sql创建分区表_大数据_04

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