hive-內部表與外部表的區別
本文以例子的形式介紹一下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 刪除外表時,外表數據依舊存在。
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 刪除外表時,外表數據依舊存在。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.