In this post let us learn how to drop partition in hive using examples
Partition in hive
Hive table can have one or multiple partition keys by which the query performance has been improved. By including the partition keys in our query , we can actually achieve the query optimization.
External and internal table in hive
Two types of tables available in hive are external and internal
For external tables , table properties need to be changed before dropping partitions .
alter table employee set tblproperties ('EXTERNAL'='FALSE');
Drop partition in hive
We can drop the respective partition of the external table using the following command .
ALTER TABLE employee set tblproperties ('EXTERNAL'='FALSE');
ALTER TABLE employee drop PARTITION (state='Bihar');
#After dropping the partition table properties should be reverted to external
ALTER TABLE employee set tblproperties ('EXTERNAL'='TRUE');
Resetting the location
We can even set the partition to the new location using the following command .
ALTER TABLE employee PARTITION(state = 'Bihar', dep = 'accounts')
SET LOCATION 'hdfs://retail/company/employee/accounts/Bihar
Dropping the range of partition in hive
For dropping range of data , we can try like below.
ALTER TABLE employee DROP PARTITION (entry_date>'2021-03-14',entry_date<'2021-12-16');
Dropping all the partitions
Following one drop all the partitions
ALTER TABLE employee DROP if exists (state<>'');
Reference
https://cwiki.apache.org/confluence/display/hive/languagemanual+ddl#LanguageManualDDL-DropPartitions