分区表
分区表在实际企业中用途非常广,首先一个案例体验一下分区表的作用:
假设我每天都要往HIVE表中插入一万条数据。经过一年的时间,我里面已经有365万条数据。
现在我需要查询某个日期的数据,我select * from tablename where data=”20180101″,那么hive一般会扫描整个表内容,会消耗很多时间。如果引入partition概念。以日期作为分区字段,相同日期的数据在同一个分区,要查询某个日期的数据,直接就定位某个分区了,就非常快。
Hive分区是在创建表的时候用Partitioned by 关键字定义的,可以定义多个分区。
hive (default)> create table partition_table(id int,age int,name string)partitioned by(sex string,data string) row format delimited fields terminated by ':';
分区表加载数据:
[root@master home]# cat partition.txt
1:12:q
2:13:a
3:14:z
[root@master home]# cat partition1.txt
4:15:w
5:16:s
load数据的时候直接指定分区,那么所有数据都会自动加上相对应的分区列。
hive (default)> load data local inpath '/home/partition.txt' into table partition_table partition(sex='man'