2013-03-19 73 views
12

如何刪除Hive表中當前加載的所有分區?刪除配置單元表中的所有分區?

我可以刪除單個分區與alter table <table> drop partition(a=, b=...);

我可以加載與恢復分區聲明所有分區。但我似乎無法刪除所有分區。

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

回答

18

從版本0.9.0開始,您可以在drop partition語句中使用comparators,該語句可用於一次性刪除所有分區。

一個例子,從drop_partitions_filter.q測試用例採取:

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

唯一情況是,它應該在非嚴格模式下完成:

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; 
0


使用來自原始表的數據創建表:

set hive.mapred.mode=nonstrict; 

我希望它有幫助。 GL!

+0

'FAILED:語義分析錯誤:1:23需要指定分區列,因爲目標表是分區的。在標記't1'附近遇到錯誤 – 2013-04-03 00:39:22

+1

@MattJoiner更正,但完全相信Balaswamy vaddeman。 – www 2013-04-03 18:57:25

3

從下面的現有表t1創建一個新表t2。

create table t2 as 
    select * from t1; 

降舊錶T1

drop table t1; 

現在檢查是否有新的表分區。

show partitions t2; 
8

蜂房允許你選擇分區時使用比較運算符(例如><=<>)。例如,以下應該刪除表中的所有分區。

ALTER TABLE table_name DROP PARTITION (partition_name > '0'); 
-2
truncate table table_name; 

將刪除所有分區。這非常有用,特別是如果你想刪除分區表。

+1

錯誤的答案請刪除它 – 2017-02-20 14:38:19