作用:
在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');