文章目录
一、分区表
1.1 分区表
Hive中的分区就是把一张大表的数据按照业务需要分散的存储到多个目录,每个目录就称为该表的一个分区。在查询时通过where子句中的表达式选择查询所需要的分区,这样的查询效率会提高很多。
1.1.1 分区表基本语法
1.1.1.1 创建分区表
create table dept_partition
(
deptno int, --部门编号
dname string, --部门名称
loc string --部门位置
)
partitioned by (day string)
row format delimited fields terminated by '\t';
1.1.1.2 分区表读写数据
1)写数据
load
在/opt/module/hive/datas/路径上创建文件dept_20220401.log,并输入如下内容
[yudan@hadoop102 datas]$ vim dept_20220401.log
10 行政部 1700
20 财务部 1800
load data local inpath '/opt/module/hive/datas/dept_20220401.log'
into table dept_partition
partition(day='20220401');
insert
将day='20220401’分区的数据插入到day='20220402’分区,可执行如下装载语句
insert overwrite table dept_partition partition (day = '20220402')
select deptno, dname, loc
from dept_partition
where day = '2020-04-01'
2)读数据
查询分区表数据时,可以将分区字段看作表的伪列,可像使用其他字段一样使用分区字段。
select deptno, dname, loc ,day
from dept_partition
where day =</