truncate table ... 删除所有数据。
truncate table partition (...) 删除特定分区的数据。
目录结构被保留。
应先将外部表转换成manged,.e.g
alter table t set tblproperties('EXTERNAL'='FALSE');
完成后,我们可以将其转换回来
alter table t set tblproperties('EXTERNAL'='TRUE');
演示
create table t (i int) partitioned by (x char(1));
set hive.exec.dynamic.partition.mode=nonstrict;
insert into t partition (x) values (1,'A'),(2,'B'),(3,'C');
alter table t set tblproperties('EXTERNAL'='TRUE');
select * from t;
+-----+-----+
| t.i | t.x |
+-----+-----+
| 1 | A |
| 2 | B |
| 3 | C |
+-----+-----+
dfs -ls -R /user/hive/warehouse/t;
drwxrwxrwx - cloudera supergroup 0 2017-03-28 11:40 /user/hive/warehouse/t/x=A
-rwxrwxrwx 1 cloudera supergroup 2 2017-03-28 11:40 /user/hive/warehouse/t/x=A/000000_0
drwxrwxrwx - cloudera supergroup 0 2017-03-28 11:40 /user/hive/warehouse/t/x=B
-rwxrwxrwx 1 cloudera supergroup 2 2017-03-28 11:40 /user/hive/warehouse/t/x=B/000000_0
drwxrwxrwx - cloudera supergroup 0 2017-03-28 11:40 /user/hive/warehouse/t/x=C
-rwxrwxrwx 1 cloudera supergroup 2 2017-03-28 11:40 /user/hive/warehouse/t/x=C/000000_0
truncate table t partition (x='B');
失败:SemanticException [错误 10146]:无法截断非托管
表 t。
alter table t set tblproperties('EXTERNAL'='FALSE');
truncate table t partition (x='B');
select * from t;
+-----+-----+
| t.i | t.x |
+-----+-----+
| 1 | A |
| 3 | C |
+-----+-----+
dfs -ls -R /user/hive/warehouse/t;
drwxrwxrwx - cloudera supergroup 0 2017-03-28 11:40 /user/hive/warehouse/t/x=A
-rwxrwxrwx 1 cloudera supergroup 2 2017-03-28 11:40 /user/hive/warehouse/t/x=A/000000_0
drwxrwxrwx - cloudera supergroup 0 2017-03-28 11:42 /user/hive/warehouse/t/x=B
drwxrwxrwx - cloudera supergroup 0 2017-03-28 11:40 /user/hive/warehouse/t/x=C
-rwxrwxrwx 1 cloudera supergroup 2 2017-03-28 11:40 /user/hive/warehouse/t/x=C/000000_0
truncate table t;
+-----+-----+
| t.i | t.x |
+-----+-----+
dfs -ls -R /user/hive/warehouse/t;
drwxrwxrwx - cloudera supergroup 0 2017-03-28 11:43 /user/hive/warehouse/t/x=A
drwxrwxrwx - cloudera supergroup 0 2017-03-28 11:43 /user/hive/warehouse/t/x=B
drwxrwxrwx - cloudera supergroup 0 2017-03-28 11:43 /user/hive/warehouse/t/x=C
alter table t set tblproperties('EXTERNAL'='TRUE');