hive 分区表 静态 动态分区
原创
©著作权归作者所有:来自51CTO博客作者塞上江南o的原创作品,请联系作者获取转载授权,否则将追究法律责任
目录
- hive outline
- 分区表的好处
- hive 静态分区
- 创建分区表&向分区表中加载数据
- 查询哪些分区
- 查询分区表中数据
- 增加分区
- hive msck 修复分区
- 删除分区
- 重命名分区表名
- 更改分区文件存储格式
- 更改分区位置
- 2级分区表简介:
- 创建二级分区表
- 查询多级分区表的数据
- hive 动态分区
- 为什么使用动态分区
- hive 动态分区表的创建
hive outline
链接
分区表的好处
为了避免查询时全表扫描数据
hive 静态分区
静态分区
指的是分区的字段值是由用户在加载数据的时候手动指定的
创建分区表&向分区表中加载数据
1.建表语句
(1)关键语句:
partitioned by (field string)
(2)分区字段不能是表中已经存在
的字段,因为分区字段最终也会显示在表结构上
(3)分区字段是虚拟字段,其数据并不存储在底层的文件中
(4)分区不同,对应数据存放的文件夹不同
create table dept_partition(
deptno int,
dname string,
loc string
)
partitioned by (month string)
row format delimited fields terminated by '\t';
2.加载数据到分区表中
load data local inpath '/opt/modules/input/dept.txt' into table dept_partition partition(month='2021-02-11');
-- 还可以通过 insert + select 的方式,向表中加载数据,参考3.

3. 再次加载数据到文件夹
--从自己中查询数据然后再写入自己的另一个分区
insert into table dept_partition partition(month = '2021-02-12') select * from dept_partition;

查询哪些分区
#dept_partition是分区表表名
show partitions dept_partition;

查询分区表中数据
hive (default)> select * from dept_partition where month='2021-02-11';

select * from dept_partition where month='2021-02-11'
union
select * from dept_partition where month='2021-02-12';
增加分区
虽然增加了分区,但是没有源数据,想要加载数据到分区中,并让元数据和源数据产生关联,有2种方案
- 使用load data命令(上边有)
- 使用msck命令
alter table dept_partition add partition(month='2021-02-13');
ALTER TABLE dept_partition
ADD IF NOT EXISTS
PARTITION ( MONTH = '2021-02-14' )
PARTITION ( MONTH = '2021-02-16' );
#查询某个表下有哪些分区 dept_partition是表名
hive (default)> show partitions dept_partition;
OK
partition
month=2021-02-11
month=2021-02-12
month=2021-02-13
month=2021-02-14
month=2021-02-16
以上可以看出不是连续的分区
hive msck 修复分区
Hive将每个表的分区列表信息存储在其metastore中。但是,如果将新分区直接添加到HDFS(例如通过使用hadoop fs -put命令)或从HDFS中直接删除分区文件夹,则除非用户ALTER TABLE table_name ADD/DROP PARTITION在每个新添加的分区上运行命令,否则metastore(也就是Hive)将不会意识到分区信息的这些更改,但是用户可以使用修复表选项运行metastore check命令
1.创建指定的子分区目录
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition/month=2021-02-21;
/user/hive/warehouse是我的hive的仓库路径
2. 上传数据到刚创建好的子分区目录中
hive (default)> dfs -put /opt/modules/input/dept.txt /user/hive/warehouse/dept_partition/month=2021-02-21;
- 查询数据
hive (default)> select * from dept_partition where month='2021-02-21';
结果:
查询不到数据
- 修复关系
msck repair table dept_partition;
- 再次查询数据
删除分区
这将删除该分区的数据和元数据
alter table dept_partition drop partition(month='2021-02-16');
ALTER TABLE dept_partition
DROP
PARTITION ( MONTH = '2021-02-11' ),
PARTITION(MONTH = '2021-02-13');
重命名分区表名
#dept_partition 旧表名 dept_partition2是新表名
alter table dept_partition rename to dept_partition4;
更改分区文件存储格式
ALTER TABLE table_name PARTITION (dt='2008-08-09') SET FILEFORMAT file_format;
更改分区位置
ALTER TABLE table_name PARTITION (dt='2008-08-09') SET LOCATION "new location";
hive中的2级分区表的创建
2级分区表简介:
实际就是多级文件夹
,一级分区是一级文件夹,二级分区时二级文件夹
创建二级分区表
1.sql
create table dept_partition2(
deptno int, dname string, loc string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t';
2.加载数据到分区表中
load data local inpath'/opt/modules/input/dept.txt' into table dept_partition2 partition (month='2021-03-11',day='01');
查询多级分区表的数据
select * from dept_partition2 where month='2021-03-11' and day='01';
hive 动态分区
动态分区
指的是分区的字段值是基于查询结果自动推断出来的。要借助核心语法
insert+select
为什么使用动态分区
当往hive分区表中插入加载数据时,如果需要创建的分区很多,则需要复制粘贴修改很多sql去执行,效率低。因为hive是批处理系统,所以hive提供了一个动态分区功能,其可以基于查询参数的位置去推断分区的名称,从而建立分区
hive 动态分区表的创建
使用动态分区前,必须做以下2步
(1)开启动态分区功能(默认 true,开启)
hive (default)> set hive.exec.dynamic.partition=true;
(2)设置为非严格模式
- 动态分区的模式,默认 strict,表示必须指定至少一个分区为静态分区
- nonstrict 模式表示允许所有的分区字段都可以使用动态分区
hive (default)>set hive.exec.dynamic.partition.mode=nonstrict;
(3) 创建目标分区表
create table dept_par(dname string,loc int)
partitioned by (deptno int)
row format delimited fields terminated by '\t';
(4) 加载数据到目标分区表
insert into table dept_par partition(deptno)
select dname,loc,deptno from dept;
- 从表dept中查询出列dname,列loc中的数据,然后插入到表dept_par中
- 再从表dept中查询出列deptno,让这一列作为表dept_par的分区字段