hive 內部表與外部表的區別

2021-07-03 09:39:25 字數 2894 閱讀 8985

本文以例子的形式介紹一下hive內錶和外表的區別。例子共有4個:不帶分割槽的內錶、帶分割槽的內錶、不帶分割槽的外表、帶分割槽的外表。

1 不帶分割槽的內錶

#建立表

create table innertable(id int,name string) row format delimited fields terminated by '|';(show tables發現沒有innertable,只有innertable。不多說,記住了)

#從hdfs上載入資料

load data inpath 'hdfs://master:9000/user/root/test/innertable' into table innertable; (檢視hdfs上/user/root/test/innertable,發現檔案價innertable還在,但是裡面的檔案已經不在了。去哪了,去innertable表中了)

#刪除剛剛建立的表

drop table innertable;(到hdfs上看一下innertable資料夾及其中的檔案都沒有了。去哪了,刪除表的時候刪除了)

2 帶分割槽的內錶

#建立表

create table inner_table_with_p(id int,name string) partitioned by (part_num int);(hdfs 出現資料夾inner_table_with_p,資料夾中為空)

#從hdfs載入資料

load data inpath 'hdfs://master:9000/user/root/test/innertable/part1' into table inner_table_with_p partition(part_num=1)(資料夾inner_table_with_p出現子資料夾part_num=1,innertable中part1消失);

load data inpath 'hdfs://master:9000/user/root/test/innertable/part2' into table inner_table_with_p partition(part_num=2)(資料夾inner_table_with_p出現子資料夾part_num=2,innertable中part2消失);

load data inpath 'hdfs://master:9000/user/root/test/innertable/part3' into table inner_table_with_p partition(part_num=3)(資料夾inner_table_with_p出現子資料夾part_num=3,innertable中part3消失);

#刪除分割槽

alter table inner_table_with_p drop partition(part_num=1);(part_num=1對應分割槽資料夾本刪除)

#刪除表

drop table inner_table_with_p;(hdfs上inner_table_with_p資料夾被刪除)

3 不帶分割槽的外表

建立表

create external table outer_table(id int,name string) row format delimited fields terminated by '|';      (hive倉儲目錄中出現outer_table)

載入資料

load data inpath '/user/root/test/outertable/outer' into table outer_table;(outer_table中出現子檔案outer,outertable中outer消失)

刪除表drop table outer_table;    (outer_table及子檔案outer依然存在,因為這是外表)

4 帶分割槽的外表

建立表create external table outer_table_with_p(id int,name string) partitioned by (part_num int) row format delimited fields terminated by '|'; (hive倉儲目錄中出現outer_table_with_p)

載入資料

load data inpath '/user/root/test/outertable/part1' into table outer_table_with_p partiton(part_num=1);  (outer_table_with_p中出現子資料夾part_num=1)

load data inpath '/user/root/test/outertable/part2' into table outer_table_with_p partition(part_num=2);(outer_table_with_p中出現子資料夾part_num=2)

load data inpath '/user/root/test/outertable/part3' into table outer_table_with_p partition(part_num=3);(outer_table_with_p中出現子資料夾part_num=3)

刪除分割槽

alter table outer_table_with_p drop partition(part_num=1);(hdfs上分割槽檔案依舊存在)

刪除表

drop table outer_table_with_p;(hdfs上對應資料依舊存在)

總結:

1 刪除內錶時,內錶資料會一併刪除;

2 刪除外表時,外表資料依舊存在。

hive外部表與內部表的區別

測試一下,放三個檔案到hdfs中 hdfs dfs mkdir input hdfs dfs put student01.txt input hdfs dfs put student02.txt input hdfs dfs put student03.txt input 現在建立乙個外部表來指向這...

hive 內部表與外部表的區別

hive 內部表 hive create table soyo55 name string,addr string,money string row format delimited fields terminated by stored as textfile hive load data loc...

Hive內部表 外部表區別

hive內部表 外部表區別自不用說,可實際用的時候還是要小心。1.內部表 sql view plain copy print create table tt name string age string location input table data 此時,會在hdfs上新建乙個tt表的資料存放...