Hive中分区表及陷阱
分区表
分区表实际就是对应hdfs文件系统上的的独立的文件夹,该文件是夹下是该分区所有数据文件。
分区可以理解为分类,通过分类把不同类型的数据放到不同的目录下。
分类的标准就是分区字段,可以一个,也可以多个。
分区表的意义在于优化查询。查询时尽量利用分区字段。如果不使用分区字段,就会全部扫描。
在查询是通过where子句查询来指定所需的分区。
样例
create external table if not exists emp_partition(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
partitioned by (month string)
row format delimited fields terminated by '\t'
操作步骤
创建分区表
create external table if not exists emp_partition(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
partitioned by (month string)
row format delimited fields terminated by '\t'
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/46292bc7ff03400bb5704d7363e512a2.webp?x-oss-process=image/resize,w_1400/format,webp)
加载数据到指定分区
load data local inpath '/opt/datas/emp.txt' into table default.emp_partition partition (month='201803');
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/ea8b356e382f43b999a0fef1cbe51f11.webp?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/376eefae93b541ba84bf064a1cb9b3e8.webp?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/6b069f742a5245c0bee18422a92d6aba.webp?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/646a93bc49d34bb68a030929817cbaa1.webp?x-oss-process=image/resize,w_1400/format,webp)
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/388aaa759146488c9981fc05ff29c520.webp?x-oss-process=image/resize,w_1400/format,webp)
分区中的数据查询
查询201803分区数据
命令:select * from emp_partition where month='201803' ;
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/594553bcc27a4997918769eaae826256.webp?x-oss-process=image/resize,w_1400/format,webp)
查询201803分区数据
命令:select * from emp_partition where month='201804' ;
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/73535feef1a3477d905c7c7a697276c6.webp?x-oss-process=image/resize,w_1400/format,webp)
201803分区与201804分区组合统计总人数
编写sql文件
sql内容
select count(ename) from emp_partition where month='201803' union all select count(ename) from emp_partition where month='201804'
执行sql文件
bin/hive -f /opt/datas/emp_partition.sql
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/c25d4ff5a66649b09c70537c4907b7a9.webp?x-oss-process=image/resize,w_1400/format,webp)
分区陷阱
创建分区表
create table dept_partition(
deptno int,
dname string,
loc string
)
partitioned by (day string)
row format delimited fields terminated by '\t';
在分区表下创建分区文件夹。
创建分区目录:
dfs -mkdir /user/hive/warehouse/dept_partition/day=20180306
把数据文件上传到分区文件夹下
通过 dfs -put /本地文件 /hdfs目录 ,完成文件上传。
dfs -put /opt/datas/dept.txt /user/hive/warehouse/dept_partition/day=20180306;
查询数据没有结果
select * from dept_partition ;
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/4f4759e78bc3439a8dfe5f6082b9c5cc.webp?x-oss-process=image/resize,w_1400/format,webp)
原因 mysql下的matehouse下的分区表中没有分区数据
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/e5dac92018cb4f0497c168f61e8a88f2.webp?x-oss-process=image/resize,w_1400/format,webp)
解决方法
msck repair table dept
说明:users can run a metastore check command with the repair table option
msck的意思就是matestore check的意思
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/5470bb8e8c624f9b92fc643bdf32dd53.webp?x-oss-process=image/resize,w_1400/format,webp)
测试结果
select * from dept_partition;
![](https://ucc.alicdn.com/qotwlfg67zs74/developer-article513814/20241019/87f1705b2ee44c29b03d88046070f757.webp?x-oss-process=image/resize,w_1400/format,webp)
为什么会出现这样的问题。
因为通过HDFS put/cp命令往表目录下拷贝分区目录数据文件时并没有在metastore中创建分区数据。所以即使你把数据copy、put到文件夹下也不会查询到。