Hive操作——刪除表(drop、truncate)
阿新 • • 發佈:2020-08-07
Hive刪除操作主要分為幾大類:刪除資料(保留表)、刪除庫表、刪除分割槽。
一、僅刪除表中資料,保留表結構
hive> truncate table 表名;
truncate操作用於刪除指定表中的所有行,相當於delete from table where 1=1.表達的是一個意思。
注意:truncate 不能刪除外部表!因為外部表裡的資料並不是存放在Hive Meta store中。建立表的時候指定了EXTERNAL,外部表在刪除分割槽後,hdfs中的資料還存在,不會被刪除。因此要想刪除外部表資料,可以把外部錶轉成內部表或者刪除hdfs檔案。
二、刪除表
hive> drop table if exists 表名;
drop table if exists table_name;
三、刪除庫
hive> drop database if exists 庫名;
注意如果庫裡有表會報錯
解決這個錯誤有兩種方法:一、就是很簡單的將所有表先刪除完,再刪除庫。
另外一種就是使用下述的方法:使用cascade關鍵字執行強制刪庫。drop database if exists 庫名 cascade;
四、刪除hive分割槽
alter table table_name drop partition (partition_name='分割槽名')