Hive-刪除表(drop、truncate的區別)


Hive刪除操作主要分為幾大類:刪除數據(保留表)、刪除庫表、刪除分區。我將以下圖為例清空iot_devicelocation中的數據,之后再刪除表、庫等。

 

 

 解釋:

 use xpu123;   #使用的庫為xpu123
 show tables;  #顯示該庫中的所有的表名

 

首先來看一下iot_deivcelocation中的數據。select * from iot_deivcelocation。

 

 

 

 

一、僅刪除表中數據,保留表結構

hive> truncate table iot_devicelocation;
truncate操作用於刪除指定表中的所有行,相當於delete from table where 1=1.表達的是一個意思。

注意:truncate 不能刪除外部表!因為外部表里的數據並不是存放在Hive Meta store中。創建表的時候指定了EXTERNAL,外部表在刪除分區后,hdfs中的數據還存在,不會被刪除。因此要想刪除外部表數據,可以把外部表轉成內部表或者刪除hdfs文件。

 

 

 

 

二、刪除表

hive> drop table if exists iot_devicelocation;
drop table if exists table_name;

 

 

 

 

三、刪除庫

hive> drop database if exists xpu123;
drop database if exists database_name;但是根據第二步操作結束,我們的數據庫xpu123中,還存在iot_deviceenergytype表,因此,如果直接刪除,會報以下錯誤。Hive會提醒你,將要執行刪除操作的xpu123的庫里面還存在tables。

 

解決這個錯誤有兩種方法:一、就是很簡單的將所有表先刪除完,再刪除庫。

另外一種就是使用下述的方法:使用cascade關鍵字執行強制刪庫。drop database if exists xpu123 cascade; 如下所示

 

 

 

 

四、刪除hive分區

alter table table_name drop partition (partition_name='分區名')

hive> alter table tablename drop partition(load_date='2019-01-01');

 

參考:https://blog.csdn.net/a_drjiaoda/java/article/details/94433005


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM