1. 分区表
分区表实际上就是对应一个 HDFS 文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive 中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集。在查询时通过 WHERE 子句中的表达式选择查询所需要的指定的分区,这样的查询效率会提高很多。
2. 分区表基本操作
1)引入分区表(需要根据日期对日志进行管理,通过部门信息模拟)

2)创建分区表语法
hive (hive3)> create table dept_par(deptno int , dname string, loc string) partitioned by (day string) row format delimited fields terminated by'';
OK
Time taken: 2.547 seconds
注意:分区字段不能是表中已经存在的数据,可以将分区字段看作表的伪列。
3)加载数据到分区表中
hive (hive3)> load data local inpath '/home/zzdq/hive/dept_20200401.log' into table dept_par partition(day='20200401');
Loading data to table hive3.dept_par partition (day=20200401)
OK
Time taken: 1.763 seconds
hive (hive3)> load data local inpath '/home/zzdq/hive/dept_20200402.log' into table dept_par partition(day='20200402');
Loading data to table hive3.dept_par partition (day=20200402)
OK
Time taken: 0.962 seconds
hive (hive3)> load data local inpath '/home/zzdq/hive/dept_20200403.log' into table dept_par partition(day='20200403');
Loading data to table hive3.dept_par partition (day=20200403)
OK
Time taken: 0.869 seconds
4)查找全部数据(可以看到多了个分区字段,但这个字段不是放在表中,而是放在目录上,所以条件查分区的效率会高很多)
hive (hive3)> select * from dept_par;
OK
dept_par.deptno dept_par.dname dept_par.loc dept_par.day
10 ACCOUNTING 1700 20200401
20 RESEARCH 1800 20200401
30 SALES 1900 20200402
40 OPERATIONS 1700 20200402
50 TEST 2000 20200403
60 DEV 1900 20200403
NULL NULL NULL 20200403
Time taken: 3.684 seconds, Fetched: 7 row(s)
5)条件查询
hive (hive3)> select * from dept_par where day = 20200401;
OK
dept_par.deptno dept_par.dname dept_par.loc dept_par.day
10 ACCOUNTING 1700 20200401
20 RESEARCH 1800 20200401
Time taken: 2.749 seconds, Fetched: 2 row

本文详细介绍了Hive中的分区表概念、创建、数据加载、条件查询、分区管理(增删查),以及二级分区和动态分区的设置和使用。通过实例展示了如何根据日期和时间对数据进行高效管理和查询。
最低0.47元/天 解锁文章
338

被折叠的 条评论
为什么被折叠?



