Drop all partitions from a hive table?
Asked Answered
C

5

14

How can I drop all partitions currently loaded in a Hive table?

I can drop a single partition with alter table <table> drop partition(a=, b=...);

I can load all partitions with the recover partitions statement. But I cannot seem to drop all partitions.

I'm using the latest Hive version supported by EMR, 0.8.1.

Cheery answered 19/3, 2013 at 5:52 Comment(0)
O
23

As of version 0.9.0 you can use comparators in the drop partition statement which may be used to drop all partitions at once.

An example, taken from the drop_partitions_filter.q testcase :

create table ptestfilter (a string, b int) partitioned by (c string, d string);
alter table ptestfilter add partition (c='US', d=1);
alter table ptestfilter add partition (c='US', d=2);
alter table ptestFilter add partition (c='Uganda', d=2);
alter table ptestfilter add partition (c='Germany', d=2);
alter table ptestfilter add partition (c='Canada', d=3);
alter table ptestfilter add partition (c='Russia', d=3);
alter table ptestfilter add partition (c='Greece', d=2);
alter table ptestfilter add partition (c='India', d=3);
alter table ptestfilter add partition (c='France', d=4);

show partitions ptestfilter;
alter table ptestfilter drop partition (c>'0', d>'0');
show partitions ptestfilter;
Outwardly answered 19/3, 2013 at 8:49 Comment(0)
J
14

Hive allows you to use comparison operators (e.g. >, <, =, <> ) when selecting partitions. For example, the following should drop all partitions in the table.

ALTER TABLE table_name DROP PARTITION (partition_name > '0');
Jacinda answered 8/11, 2014 at 1:11 Comment(0)
F
3

create a new table t2 from existing table t1 like below.

 create table t2 as
    select * from t1;

drop old table t1

drop table t1;

now check if you have partitions on new table.

show partitions t2;
Fewer answered 3/4, 2013 at 12:6 Comment(0)
R
0


Create table using data from original table:

CREATE TABLE t2 AS
SELECT column_name_1, ..., column_name_N FROM t1;

Only case is that it should be done in non-strict mode:

set hive.mapred.mode=nonstrict;

I hope it helps. GL!

Ripply answered 19/3, 2013 at 13:1 Comment(2)
FAILED: Error in semantic analysis: 1:23 Need to specify partition columns because the destination table is partitioned. Error encountered near token 't1'Cheery
@MattJoiner Corrected, but full credit goes to Balaswamy vaddeman.Ripply
S
-3
truncate table table_name; 

will delete all the partitions. This is useful especially if you want to drop partitioned table.

Sweetie answered 12/2, 2016 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.