天天看點

hive内部表和外部表的差別

内部表&外部表

未被external修飾的是内部表(managed table)可省略,被external修飾的為外部表(external table);

差別:

内部表資料由Hive自身管理,外部表資料由HDFS管理;

内部表資料存儲的位置是hive.metastore.warehouse.dir(預設:/user/hive/warehouse)加location '/input/table_data';指定路徑,外部表資料的存儲位置由自己制定;

删除内部表會直接删除中繼資料(metadata)及存儲資料;删除外部表僅僅會删除中繼資料,HDFS上的檔案并不會被删除;

對内部表的修改會将修改直接同步給中繼資料,而對外部表的表結構和分區進行修改,則需要修複(MSCK REPAIR TABLE table_name;)

如下,進行試驗進行了解

建立内部表t1

create table t1(
    id      int
   ,name    string
   ,hobby   array<string>
   ,add     map<String,string>
)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':'
;      

2. 檢視表的描述:desc t1; 

3.上傳資料:

load data local inpath '/home/hadoop/Desktop/data' overwrite into table t1;      
load data local inpath '/home/hadoop/Desktop/data' overwrite into table t1;      

local:複制上傳,從Linux本地檔案系統上傳到表格

無local:剪切上傳,從hdfs檔案系統上傳到表格

建立一個外部表t2

create external table t2(
    id      int
   ,name    string
   ,hobby   array<string>
   ,add     map<String,string>
)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':'
location '/user/t2'
;      

因而外部表僅僅删除中繼資料

繼續閱讀