为什么要分区?
随着系统运行时间增长,表的数据量越来越大,而hive查询通常是全表扫描,这样会导致大量不必要的数据扫描,从而大大降低了查询效率。
从而引进了分区技术,使用分区技术,避免hive全表扫描,提升查询效率。
分区的技术
PARTITIONED BY (column_name data_type)
1、hive分区是区分大小写的
2、hive的分区本质是在表目录下创建分区目录,但是该分区字段是一个伪列,不真实存在于数据中。age=19
3、一张表中可以有一个或多个分区,分区下面也可以有一个或者多个分区。
如何分区?
根据业务需求,通常采用年、月、日、地区等。
分区的意义
可以让用户在做数据统计时缩小数据扫描的范围,因为可以在select时指定分区条件
创建一层分区
create table if not exists part1(
uid int,
uname string,
uage int
)
PARTITIONED BY (country string)
row format delimited
fields terminated by ','
;
分区表的数据导入方式:part1表名 然后指定分区partition(country='china'); 分区目录
将文件从哪里上传到哪个表里面,然后指定分区
load data local inpath '/hivedata/stu1.txt' into table part1 partition(country='china');
load data local inpath '/hivedata/stu_japan.txt' into table part1 partition(country='japan');
load data local inpath '/hivedata/stu1.txt' into table part1 partition(COUNTRY='CHINA');
做分区一定要带条件查询(内容是区分大小写的)
字段等于分区的内容
创建二级分区:
create table if not exists part2(
uid int,
uname string,
uage int
)
PARTITIONED BY (year string,month string)
row format delimited
fields terminated by ','
;
加载数据:
load data local inpath '/hivedata/stu1.txt' into table part2 partition(year='2019',month='07');
load data local inpath '/hivedata/stu_japan.txt' into table part2 partition(year='2019',month=08);
load data local inpath '/hivedata/stu1.txt' into table part2 partition(year='2018',month='07');
load data local inpath '/hivedata/stu1.txt' into table part2 partition(year='2019',month='09');
load data local inpath '/hivedata/stu1.txt' into table part2 partition(year='2019',month='06');
创建三级分区:
create table if not exists part3(
uid int,
uname string,
uage int
)
PARTITIONED BY (year string,month string,day string)
row format delimited
fields terminated by ','
;
加载数据:
load data local inpath '/hivedata/stu1.txt' into table part3 partition(year='2019',month='07',day='19');
load data local inpath '/hivedata/stu_japan.txt' into table part3 partition(year