1. 程式人生 > >HIVE刪除外部表

HIVE刪除外部表

測試

使用truncate命令刪除外部表:

truncate table mytable;
#FAILED: SemanticException [Error 10146]: Cannot truncate non-managed table mytable. (state=,code=0)

分析

查看錶結構
1. describe extended tablename
2. desc formatted tablename;

                               | NULL                                               | NULL
| | # Partition Information | NULL | NULL | | # col_name | data_type | comment | | | NULL | NULL
| | load_date | string | | | | NULL | NULL | | # Detailed Table Information | NULL | NULL
| | Database: | ods_project | NULL | | Owner: | hive | NULL | | CreateTime: | Wed Jul 25 16:24:48 CST 2018 | NULL | | LastAccessTime: | UNKNOWN | NULL | | Protect Mode: | None | NULL | | Retention: | 0 | NULL | | Location: | hdfs://***/myname | NULL | | Table Type: | EXTERNAL_TABLE | NULL | | Table Parameters: | NULL | NULL | | | EXTERNAL | TRUE | | | numPartitions | 1 | | | transient_lastDdlTime | 1532507088 | | | NULL | NULL | | # Storage Information | NULL | NULL | | SerDe Library: | org.apache.hadoop.hive.ql.io.orc.OrcSerde | NULL | | InputFormat: | org.apache.hadoop.hive.ql.io.orc.OrcInputFormat | NULL | | OutputFormat: | org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat | NULL | | Compressed: | No | NULL | | Num Buckets: | -1 | NULL | | Bucket Columns: | [] | NULL | | Sort Columns: | [] | NULL | | Storage Desc Params: | NULL | NULL | | | serialization.format | 1 | +-------------------------------+----------------------------------------------------+-----------------------+--

原因

truncate不能刪除外部表,只能刪除內部表

刪除外部表

1.刪除該表分割槽:

alter table tablename drop partition(load_date='2018-11-23',p_hou16);

2.刪除hdfs中的資料

[[email protected] ~]$ hadoop fs -ls /home/tablename
Found 8 items
drwxr-x---   - root users          0 2018-09-27 13:39 /home/tablename/p_date=2018-03-19
drwxr-x---   - root users          0 2018-09-27 15:24 /home/tablename/p_date=2018-03-20

可以將外部表變為內部表,再刪除內部表

ALTER TABLE xxx SET TBLPROPERTIES('EXTERNAL'='False'); 
drop table xxx;

總結

外部表:建立表的時候指定了EXTERNAL,外部表在刪除分割槽後,hdfs中的資料還存在,不會被刪除

內部表:建立表的時候未指定,直接使用drop就能把hdfs裡的資料刪掉