从Hive表中删除所有分区?

14

我如何删除Hive表中当前加载的所有分区?

我可以使用alter table <table> drop partition(a=, b=...);命令来删除单个分区。

我可以使用“recover partitions”语句加载所有分区。但是我似乎无法删除所有分区。

我正在使用EMR支持的最新Hive版本0.8.1。

5个回答

23
从0.9.0版本开始,您可以在删除分区语句中使用比较器,这可以用于一次性删除所有分区。
下面是一个例子,取自drop_partitions_filter.q测试用例:
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;

14

Hive允许您在选择分区时使用比较运算符(例如><=<>)。例如,以下语句应删除表中的所有分区。

ALTER TABLE table_name DROP PARTITION (partition_name > '0');

3
从现有表t1创建一个新表t2,格式如下。
 create table t2 as
    select * from t1;

删除旧表 t1

drop table t1;

现在检查一下新表上是否有分区。

show partitions t2;

0


使用原始表中的数据创建表:

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

唯一的情况是它必须在非严格模式下完成:

set hive.mapred.mode=nonstrict;

希望能对你有所帮助。祝好运!


FAILED: Error in semantic analysis: 1:23 Need to specify partition columns because the destination table is partitioned. Error encountered near token 't1' - Matt Joiner
1
@MattJoiner 已经更正,但全部功劳归于 Balaswamy vaddeman。 - www

-3
truncate table table_name; 

将删除所有分区。如果您想删除分区表,这将非常有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接