Hive內部表和外部表

Hive表分爲內部表和外部表vim

Hive默認創建的表是內部表,內部表create以後,而後load加載hdfs上的數據,會移動物理數據到Hive的數據倉庫默認目錄(/user/hive/warehouse/xx.db/)下。網站

內部表drop以後,元數據和物理數據都會刪除。spa

外部表在導入hdfs的數據後,數據並無移動到本身的數據倉庫目錄下,也就是說外部表中的數據並非由它本身來管理的!3d

外部表drop掉以後,元數據刪掉了,可是實際物理數據還存在原位置。code

 

如下是示例:orm

在本地創建vim一個course.txt文件blog

上傳到hdfs文件系統上inner目錄下和ext目錄下qt

[root@master hiveTest]# hdfs dfs -put ./course.txt /inner
[root@master hiveTest]# hdfs dfs -put ./course.txt /ext

hive建立內部表數據分析

hive> create table db_hive.courseInner(id int,course string) row format delimited fields terminated by ',';

加載hdfs的inner目錄下的course.txt到內部表string

hive> load data inpath '/inner/course.txt' into table db_hive.courseInner;

此時,inner目錄下的文件已經被移到了hive數據倉庫的目錄/user/hive/warehouse/db_hive.db/下。

[root@master hiveTest]# hdfs dfs -ls /user/hive/warehouse/db_hive.db

而後執行drop table db_hive.courseInner,則該目錄及目錄下的數據被刪除。


 

Hive建立外部表,關鍵字external,location指定數據存放的文件夾。

hive> create external table courseext(id int,course string) row format delimited fields terminated by ',' location '/external'

此時courseex外部t表就已經連接到這個物理數據,不須要執行load語句了。

執行查詢語句,能夠查到外部表的數據。

 

外部表執行drop table courseext,則該表的元數據被刪除,但物理數據依然存在,能夠經過命令驗證。

[root@master hiveTest]# hdfs dfs -ls /external

 

使用場景:

天天收集到的網站數據,須要作大量的統計數據分析,因此在數據源上可使用外部表進行存儲,方便數據的共享。

在作統計分析時候用到的中間表,結果表可使用內部表,由於這些數據不須要共享,使用內部表更爲合適。

相關文章
相關標籤/搜索