hive中內部表和外部表的區別

2021-07-29 10:07:42 字數 1760 閱讀 2320

1.在hive裡面建立乙個表:

hive> create table wyp(id int,

> name string,

> age int,

> tele string)

> row format delimited

> fields terminated by '\t'

> stored as textfile;

oktime taken: 0.759 seconds

2.這樣我們就在hive裡面建立了一張普通的表,現在給這個表匯入資料:

load data local inpath '/home/wyp/data/wyp.txt' into table wyp;

3.建立外部表多了external關鍵字說明以及location 『/home/wyp/external』

hive> create external table exter_table(

> id int,

> name string,

> age int,

> tel string)

> location '/home/wyp/external';

oktime taken: 0.098 seconds

建立外部表,需要在建立表的時候加上external關鍵字,同時指定外部表存放資料的路徑(當然,你也可以不指定外部表的存放路徑,這樣hive將 在hdfs上的/user/hive/warehouse/資料夾下以外部表的表名建立乙個資料夾,並將屬於這個表的資料存放在這裡)

外部表匯入資料和內部表一樣:load data local inpath '/home/wyp/data/wyp.txt' into table exter_table;

4.和上面的匯入資料到表一樣,將本地的資料匯入到外部表,資料也是從本地檔案系統複製到hdfs中/home/hdfs/wyp.txt檔案中,但是,最後 資料不是移動到外部表的/user/hive/warehouse/exter_table資料夾中(除非你建立表的時候沒有指定資料的存放路徑)!大家 可以去hdfs上看看!對於外部表,資料是被移動到建立表時指定的目錄(本例是存放在/home/wyp/external資料夾中)!

5.內部表刪除

hive> drop table wyp;

moved: 'hdfs://mycluster/user/hive/warehouse/wyp' to

trash at: hdfs://mycluster/user/hdfs/.trash/current

oktime taken: 2.503 seconds

如果你要刪除外部表:drop table exter_table;

hive> drop table exter_table;

oktime taken: 0.093 seconds

和上面刪除hive的表對比可以發現,沒有輸出將資料從乙個地方移到任乙個地方!那是不是刪除外部表的的時候資料直接被刪除掉呢?答案不是這樣的,你會發現刪除外部表的時候,資料並沒有被刪除,而只是刪除了元資料,這是和刪除表的資料完全不一樣的

總結:1、在匯入資料到外部表,資料並沒有移動到自己的資料倉儲目錄下,也就是說外部表中的資料並不是由它自己來管理的,而表則不一樣;

2、在刪除表的時候,hive將會把屬於表的元資料和資料全部刪掉;而刪除外部表的時候,hive僅僅刪除外部表的元資料,資料是不會刪除的!

那麼,應該如何選擇使用哪種表呢?在大多數情況沒有太多的區別,因此選擇只是個人喜好的問題。但是作為乙個經驗,如果所有處理都需要由hive完成,那麼你應該建立表,否則使用外部表!

hive內部表和外部表的區別 內部表和外部表

內部表 create table if not exists table name刪除表時,元資料與資料都會被刪除 外部表 create external table if not exists table name location hdfs path刪除外部表只刪除metastore的元資料,不...

Hive內部表和外部表區別

建立內部表 建立 create table art inn sentence string row format delimited fields terminated by n 匯入 建立外部表 注意 外部表是在建立的時候定義實體資料的位置的,而且位置必須為資料夾,不能為檔案。1 匯入資料時 在匯...

Hive內部表和外部表的區別

建立 內部表 內部表直接建立,不需要加關鍵字 create table ifnot exits xm testa kehumc varchar 50 comment 客戶名稱 kehuzh varchar 50 comment 客戶號 comment 客戶資訊表 外部表 外部表的建立需要加上exte...