hive中清空外部表的三種方式


本文總結hive中清空外部表的三種方式

hive版本:2.1.1

環境准備

新建一張外部表:

create external table test_external (name String,age int,sex String) stored as orc;

插入數據:

insert into table test_external values("johnson",18,"男");

查看數據:

img

如果此時使用truncate 命令的話,會拋出錯誤信息 FAILED: SemanticException [Error 10146]: Cannot truncate non-managed table test_external.

img

那如果在實際場景中,需要去清空外部表,我們該怎么辦呢?

方式一:將外部表文件所在目錄指定成一個空的目錄

alter table test_external set location 'hdfs://bd227:8020/opt/hive/warehouse/test_external_like';

img

注:此方式並沒有清空外部表之前所指定路徑下的文件。

方式二:使用命令 set TBLPROPERTIES('EXTERNAL'='false') 將外部表變為內部表后,執行truncate命令,然后再更改為外部表

1:alter table test_external set TBLPROPERTIES('EXTERNAL'='false');

img

此時查看建表語句,external關鍵字已不存在,說明已變成了受hive meta store 管理的內部表

2:truncate table test_external;

執行truncate 命令,將表清空,查看hdfs上對應表的路徑下,文件也一並被清空

3:alter table test_external set TBLPROPERTIES('EXTERNAL'='true');

將表屬性更改為外部表 set TBLPROPERTIES('EXTERNAL'='true')

img

方式三:使用 insert overwrite 語句代替實現 truncate 功能

1:新建一張臨時表 test_external_temp; 該表結構與外部表的表結構一樣。

create temporary table test_external_temp (name String,age int,sex String) stored as orc;

img

注意:該臨時表只對當前會話有效。倘若你創建了臨時表,重新打開一個hive cli,此時你找不到這張表

2:執行 insert overwrite table test_external select * from test_external_temp; 使用overwrite 關鍵字執行了清空表操作


免責聲明!

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



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