[toc]

分区表

分区表 : 对应一个 HDFS 文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件

Hive 的分区 : 分目录,把一个大的数据集根据业务分割成小的数据集

分区表基本操作

引入分区表(需日期对日志进行管理, 通过部门信息模拟)

  1. dept_20210401.log
  2. dept_20210402.log
  3. dept_20210403.log
  4. ……

创建分区表语法

create table dept partition(
deptno int, 
dname string, 
loc string
)
partitioned by (day string)
row format delimited fields terminated by '\t';

分区字段不能是表中存在字段,将分区字段比作表的伪列

加载数据到分区表中

数据准备

vim dept_20210401.log

dept_20210401.log

10    ACCOUNTING    1700
20    RESEARCH    1800

dept_20210402.log

30    SALES    1900
40    OPERATIONS    1700

dept_20210403.log

50    TEST    2000
60    DEV    1900

加载数据

load data local inpath '/opt/module/hive-3.1.2/datas/dept_20210401.log' into table dept_partition partition(day='20210401');
load data local inpath '/opt/module/hive-3.1.2/datas/dept_20210402.log' into table dept_partition partition(day='20210402');
load data local inpath '/opt/module/hive-3.1.2/datas/dept_20210403.log' into table dept_partition partition(day='20210403');

分区表加载数据时,必须指定分区

查询分区表中数据

单分区查询

select * 
from 
    dept_partition 
where 
    day = '20200401';

多分区联合查询

select * from dept_partition where day='20200401'
union
select * from dept_partition where day='20200402'
union
select * from dept_partition where day='20200403';
select * 
from 
    dept_partition 
where 
    day='20200401' or day='20200402' or day='20200403';

增加分区

创建单个分区

alter table dept_partition add partition(day = '20200404');

同时创建多个分区

alter table dept_partition add partition(day = '20200405') partition(day = '20200406');

删除分区

删除单个分区

alter table dept_partition drop partition (day = '20200406');

同时删除多个分区

 alter table dept_partition drop partition (day = '20200404'), partition(day = '20200405');

查看分区表有多少分区

show partitions dept_partition;

查看分区表结构

desc formatted dept_partition;

二级分区

当一天的日志数据量很大,可再进行拆分

创建二级分区表

create table dept_partition2(
deptno int, 
dname string, 
loc string
)
partitioned by (day string, hour string)
row format delimited fields terminated by '\t';

加载数据

load data local inpath '/opt/module/hive-/datas/dept_20200401.log' into table dept_partition2 partition(day='20200401', hour='12');

查询分区数据

select * from dept_partition2 where day = '20200401' and hour = '12';

分区表和数据关联

上传数据后修复

上传数据

dfs -mkdir -p /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=13;
dfs -put /opt/module/datas/dept_20200401.log  /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=13;

查询数据

select * 
from dept_partition2 
where day = '20200401' and hour = '13';

执行修复命令

msck repair table dept_partition2;

查询数据

select * 
from dept_partition2 
where day='20200401' and hour='13';

上传数据后添加分区

上传数据 :

dfs -mkdir -p /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=14;
dfs -put /opt/module/hive/datas/dept_20200401.log /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=14;

添加分区

alter table dept_partition2 add partition(day = '201709', hour = '14');

查询数据

select * from 
    dept_partition2 
where 
    day = '20200401' and hour = '14';

load数据到分区

创建目录 :

dfs -mkdir -p /user/hive/warehouse/mydb.db/dept_partition2/day=20200401/hour=15;

上传数据 :

load data local inpath '/opt/module/hive/datas/dept_20200401.log' into table dept_partition2 partition(day = '20200401', hour = '15');

查询数据 :

select * 
from dept_partition2 
where day = '20200401' and hour = '15';

动态分区

关系型数据库中,对分区表 Insert 时,数据库会根据分区字段,自动将数据插入到相应的分区中

Hive 也有机制 (动态分区 ) ( Dynamic Partition )

开启动态分区

开启动态分区功能(默认 true,开启)

hive.exec.dynamic.partition=true

设置为非严格模式(动态分区的模式,默认 strict : 必须指定至少一个分区为静态分区,nonstrict 模式 : 允许所有的分区字段都可以使用动态分区

hive.exec.dynamic.partition.mode=nonstrict

所有执行 MR 的节点上,最大一共可以创建多少个动态分区 ( 默认1000 )

hive.exec.max.dynamic.partitions=1000

在每个执行 MR 的节点上,最大可以创建多少个动态分区

如:源数据中包含了一年的数据,即 day 字段有 365 个值,就需要设置成 > 365,如果使用默认值 100,就报错

hive.exec.max.dynamic.partitions.pernode=100

整个 MR Job ,最大可以创建多少个 HDFS 文件 ( 默认100000 )

hive.exec.max.created.files=100000

当有空分区生成时,是否抛出异常 ( 默认 false )

hive.error.on.empty.partition=false

创建目标分区表

将 dept 表中的数据按照地区 ( loc ),插入到目标表 dept_partition 的相应分区中

create table dept_partition_dy(
id int,
name string
) 
partitioned by (loc int) 
row format delimited fields terminated by '\t';

设置动态分区

set hive.exec.dynamic.partition.mode = nonstrict
insert into table dept_partition_dy partition(loc) select deptno, dname, loc from dept;

查看分区表的分区

show partitions dept_partition;

分桶表

分区提供一个隔离数据和优化查询

分桶 : 将数据集分解成更容易管理的若干部分

分区 : 针对数据的存储路径

分桶 : 针对数据文件

创建分桶表

数据准备

1001    ss1
1002    ss2
1003    ss3
1004    ss4
1005    ss5
1006    ss6
1007    ss7
1008    ss8
1009    ss9
1010    ss10
1011    ss11
1012    ss12
1013    ss13
1014    ss14
1015    ss15
1016    ss16
create table stu_bucket(
id int, 
name string
)
clustered by(id) into 4 buckets
row format delimited fields terminated by '\t';

查看表结构

desc formatted stu_bucket;

导入数据到分桶表中,load 的方式

load data inpath '/student.txt' into table stu_bucket;

查看创建的分桶表中是否分成4个桶

查询分桶的数据

select * from stu_buck;

分桶规则:

  • Hive 的分桶 : 对分桶字段的值进行哈希,除以桶的个数求余 , 来决定该条记录存放在哪个桶

分桶表操作注意点 :

  • reduce 个数 : -1, Job 会自行用多少个 reduce 或 reduce 的个数 >= 分桶表的桶数
  • 从 hdfs 中 load 数据到分桶表中,避免本地文件找不到问题
  • 不使用本地模式

insert 方式将数据导入分桶表

insert into table stu_buck select * from student_insert;

抽样查询

当大的数据集,只需要具有代表性的查询结果而不是全部结果 , 就可以对表进行抽样

语法 :

TABLESAMPLE(BUCKET x OUT OF y)

查询表 stu_buck 中的数据

select * from stu_buck tablesample(bucket 1 out of 4 on id);

x 的值必须小于等于 y 的值,否则报错 :

FAILED: SemanticException [Error 10061]: Numerator should not be bigger than denominator in sample clause for table stu_buck