Hive表中Partition的创建
作用:
在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作。有时候只需要扫描表中关心的一部分数据,在对应的partition里面去查找就可以,减少查询时间。
1. 创建表
]# cat create_rating_table_p.sql create external table rating_table_p (userId STRING, movieId STRING, rating STRING ) partitioned by (dt STRING) row format delimited fields terminated by '\t' lines terminated by '\n';
2. 导入数据
LOAD DATA LOCAL INPATH '/usr/local/hive/test/hive_test_3/ml-latest-small/2009-12.data' OVERWRITE INTO TABLE rating_table_p partition(dt='2009-12'); LOAD DATA LOCAL INPATH '/usr/local/hive/test/hive_test_3/ml-latest-small/2003-09.data' OVERWRITE INTO TABLE rating_table_p partition(dt='2003-09');
3. HDFS上面查看,会在以表名为文件夹下面,有两个以时间命名的文件夹,对应日期数据存在对应文件夹下面
]$ hdfs dfs -ls /user/hive/warehouse/rating_table_p Found 2 items drwxrwxrwx - hadoop supergroup 0 2018-06-25 15:27 /user/hive/warehouse/rating_table_p/dt=2003-10 drwxrwxrwx - hadoop supergroup 0 2018-06-25 15:26 /user/hive/warehouse/rating_table_p/dt=2009-12
4. Hive表中查询
hive> select userid, dt from rating_table_p where dt='2009-12' limit 10; OK 1 2009-12 1 2009-12 1 2009-12 1 2009-12 1 2009-12 1 2009-12 1 2009-12 1 2009-12 1 2009-12 1 2009-12
5. 删除分区
alter table rating_table_p drop if exists partition(dt='2003-10');
6.添加分区
alter table rating_table_p add if not exists partition(dt='2003-10');