Hive表分為內部表和外部表
Hive默認建立的表是內部表,內部表create之后,然后load加載hdfs上的數據,會移動物理數據到Hive的數據倉庫默認目錄(/user/hive/warehouse/xx.db/)下。
內部表drop之后,元數據和物理數據都會刪除。
外部表在導入hdfs的數據后,數據並沒有移動到自己的數據倉庫目錄下,也就是說外部表中的數據並不是由它自己來管理的!
外部表drop掉之后,元數據刪掉了,但是實際物理數據還存在原位置。
以下是示例:
在本地建立vim一個course.txt文件
上傳到hdfs文件系統上inner目錄下和ext目錄下
[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到內部表
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
使用場景:
每天收集到的網站數據,需要做大量的統計數據分析,所以在數據源上可以使用外部表進行存儲,方便數據的共享。
在做統計分析時候用到的中間表,結果表可以使用內部表,因為這些數據不需要共享,使用內部表更為合適。