大数据从0到1的完美落地之Hive分区


分区简介

为什么分区

Hive的Select查询时,一般会扫描整个表内容。随着系统运行的时间越来越长,表的数据量越来越大,而hive查询做全表扫描,会消耗很多时间,降低效率。而有时候,我们需求的数据只需要扫描表中的一部分数据即可。这样,hive在建表时引入了partition概念。即在建表时,将整个表存储在不同的子目录中,每一个子目录对应一个分区。在查询时,我们就可以指定分区查询,避免了hive做全表扫描,从而提高查询效率。

分区案例

一级分区的使用

1) 建表语句

create table if not exists part1(
id int,
name string,
age int
)
partitioned by (dt string)
row format delimited 
fields terminated by ','
lines terminated by '\n';
复制代码

2) 加载数据

load data local inpath './data/user1.txt' into table part1 partition(dt='2020-05-05');
load data local inpath './data/user2.txt' into table part1 partition(dt='2020-05-06');
复制代码

二级分区的使用

1) 建表语句

create table if not exists part2(
id int,
name string,
age int
)
partitioned by (year string,month string)
row format delimited 
fields terminated by ',';
复制代码

2) 加载数据

load data local inpath './data/user1.txt' into table part2 partition(year='2020',month='03'); 
load data local inpath './data/user2.txt' into table part2 partition(year='2020',month=04);
load data local inpath './data/user2.txt' into table part2 partition(year='2020',month="05");
复制代码

三级分区的使用

1) 建表语句

create table if not exists part3(
id int,
name string,
age int
)
partitioned by (year string,month string,day string)
row format delimited 
fields terminated by ',';
复制代码

2)加载数据

load data local inpath './data/user1.txt' into table part3 partition(year='2020',month='05',day='01');

load data local inpath './data/user2.txt' into table part3 partition(year='2019',month='12',day='31');  
复制代码

分区类型详解

分区的种类

  1. 静态分区:直接加载数据文件到指定的分区,即静态分区表。
  2. 动态分区:数据未知,根据分区的值来确定需要创建的分区(分区目录不是指定的,而是根据数据的值自动分配的)
  3. 混合分区:静态和动态都有。

分区属性设置

hive.exec.dynamic.partition=true,是否支持动态分区操作
hive.exec.dynamic.partition.mode=strict(四最可特)/nonstrict:  严格模式/非严格模式
hive.exec.max.dynamic.partitions=1000: 总共允许创建的动态分区的最大数量
hive.exec.max.dynamic.partitions.pernode=100:in each mapper/reducer node
复制代码

创建动态分区的案例

1)创建动态分区表

create table dy_part1(
sid int,
name string,
gender string,
age int,
academy string
)
partitioned by (dt string)
row format delimited fields terminated by ','
;
复制代码

2)动态分区加载数据

下面方式不要用,因为不是动态加载数据

load data local inpath '/hivedata/user.txt' into table dy_part1 partition(dt='2020-05-06');
复制代码

正确方式:要从别的表中加载数据

第一步:先创建临时表:

create table temp_part1(
sid int,
name string,
gender string,
age int,
academy string,
dt string
)
row format delimited 
fields terminated by ','
;
复制代码

注意:创建临时表时,必须要有动态分区表中的分区字段。

第二步:导入数据到临时表:

95001,李勇,男,20,CS,2017-8-31
95002,刘晨,女,19,IS,2017-8-31
95003,王敏,女,22,MA,2017-8-31
95004,张立,男,19,IS,2017-8-31
95005,刘刚,男,18,MA,2018-8-31
95006,孙庆,男,23,CS,2018-8-31
95007,易思玲,女,19,MA,2018-8-31
95008,李娜,女,18,CS,2018-8-31
95009,梦圆圆,女,18,MA,2018-8-31
95010,孔小涛,男,19,CS,2017-8-31
95011,包小柏,男,18,MA,2019-8-31
95012,孙花,女,20,CS,2017-8-31
95013,冯伟,男,21,CS,2019-8-31
95014,王小丽,女,19,CS,2017-8-31
95015,王君,男,18,MA,2019-8-31
95016,钱国,男,21,MA,2019-8-31
95017,王风娟,女,18,IS,2019-8-31
95018,王一,女,19,IS,2019-8-31
95019,邢小丽,女,19,IS,2018-8-31
95020,赵钱,男,21,IS,2019-8-31
95021,周二,男,17,MA,2018-8-31
95022,郑明,男,20,MA,2018-8-31

load data local inpath './data/student2.txt' into table temp_part1;    
复制代码

第三步:动态加载到表

insert into dy_part1 partition(dt) select sid,name,gender,age,academy,dt from temp_part1;


注意:严格模式下,给动态分区表导入数据时,分区字段至少要有一个分区字段是静态值
     非严格模式下,导入数据时,可以不指定静态值。
复制代码

混合分区示例

1) 创建一个分区表:

create table dy_part2(
id int,
name string
)
partitioned by (year string,month string,day string)
row format delimited fields terminated by ','
;
复制代码

2) 创建临时表

create table temp_part2(
id int,
name string,
year string,
month string,
day string
)
row format delimited fields terminated by ','
;

数据如下:
1,廉德枫,2019,06,25
2,刘浩(小),2019,06,25
3,王鑫,2019,06,25
5,张三,2019,06,26
6,张小三,2019,06,26
7,王小四,2019,06,27
8,夏流,2019,06,27

load data local inpath './data/temp_part2.txt' into table temp_part2;
复制代码

3) 导入数据到分区表

- 错误用法:
    insert into dy_part2 partition (year='2019',month,day) 
    select * from temp_part2;

- 正确用法:
insert into dy_part2 partition (year='2020',month,day) 
select id,name,month,day from temp_part2;
复制代码

4) 分区表注意事项

1. hive的分区使用的是表外字段,分区字段是一个伪列,但是分区字段是可以做查询过滤。
2. 分区字段不建议使用中文
3. 一般不建议使用动态分区,因为动态分区会使用mapreduce来进行查询数据,如果分区数据过多,导致namenode和resourcemanager的性能瓶颈。所以建议在使用动态分区前尽可能预知分区数量。
4. 分区属性的修改都可以修改元数据和hdfs数据内容。
复制代码

5) Hive分区和Mysql分区的区别

mysql分区字段用的是表内字段;而hive分区字段采用表外字段。

 更多大数据精彩内容欢迎B站搜索“千锋教育”或者扫码领取全套资料 

【千锋教育】大数据开发全套教程,史上最全面的大数据学习视频

猜你喜欢

转载自blog.csdn.net/longz_org_cn/article/details/131390287
今日推荐