一、外部表的创建、load、删除
1.文件在本地而不是在hdfs的时候
1.1 建表时指定location
(1)建表并load数据
create external table exTable28 (id int,str string) row format delimited fields terminated by ' ' location '/zgm/test/';
load data local inpath '/zgm2/exe28' into table exTable28;
结果:
在hdfs的/zgm/test下出现了exe28的文件
(2)删除表
drop table extable28;
在hdfs的/zgm/test的exe28文件还在
1.1 建表时不指定location
(1)建表并load数据
create external table exTable28_2 (id int,str string) row format delimited fields terminated by ' ';
load data local inpath '/zgm2/exe28' into table exTable28_2;
在hdfs 的/user/hive/warehouse/extable28_2下出现了exe28的文件
(2)删除表
在hdfs 的/user/hive/warehouse/extable28_2的exe28文件还在
2、文件在hdfs而不是在本地的时候
2.1 建表时指定location
(1)建表并load数据
create external table exTable28_3 (id int,str string) row format delimited fields terminated by ' ' location '/zgm/test2/';
load data inpath '/data/exe28' into table exTable28_3;
结果:/zgm/test2下有exe28文件,而文件本来存放的地方: /data下面没有了
(2)删除表
文件还在/zgm/test2下面
2.2 建表时不指定location
(1)建表并load数据
create external table exTable28_4 (id int,str string) row format delimited fields terminated by ' ';
load data inpath '/data/exe28' into table exTable28_4;
结果/user/hive/warehouse/extable28_4下出现了exe28的文件而文件本来存放的地方: /data下面没有了
(2)删除表
文件还在/user/hive/warehouse/extable28_4下面
总结:
1、外部表建表的时候如果是指定了location那么表就在指定的location下面,如果没有指定那么和内部表一样,在/user/hive/warehouse/下;
2、删除外部表并不会让数据消失
ps:load数据的时候,如果原数据在本地不是在hdfs上,那么数据被copy到表路径下,如果源数据在hdfs上,那么就会move到表路径下
二、内部表、外部表的转化
alter table extable28_4 set tblproperties ('external'='TRUE');//好像写成小写不会生效?
alter table extable28_4 set tblproperties ('external'='false');
alter table extable28_4 set tblproperties ('external'='FALSE');//大小写都可以
三、外部表分区表
create external table exTable28_4 (id int,str string) partitioned by (dt string)row format delimited fields terminated by ' ';
直接load的话,那么数据就从原来存储的地方,mv到了表的目录下
load data inpath '/data/exe20200103' into table extable28_4 partition(dt='20200103');
改良:这样操作,数据还在原来的地方,这样别的人也可以在数据原来的地方找到数据!
alter table add partition (dt='20200102')
location '/data/exe28_20200102';