分区的概念和分区表:
分区表指的是在创建表时指定分区空间,实际上就是在hdfs上表的目录下再创建子目录。
在使用数据时如果指定了需要访问的分区名称,则只会读取相应的分区,避免全表扫描,提高查询效率。
作用:进行分区裁剪,避免全表扫描,减少MapReduce处理的数据量,提高效率。
1.建立分区表
create external table students_pt1
(
id bigint
,name string
,age int
,gender string
,clazz string
)
PARTITIONED BY(pt string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LOCATION '/student/input1';
2.增加分区
alter table students_pt1 add partition(pt='**********');
3.删除分区
alter table students_pt1 drop partition(pt='***********')
4.查看某个表的所有分区
show partitions students_pt1;
5.往分区中插入数据
insert into table students_pt1 partition(pt='**********') select * from student1;
6.查询某个分区的数据,使用where条件进行分区裁剪,避免了全表扫描,效率高。
select count(*) from students_pt1 where pt='***********';