1,現(xiàn)象演示:
創(chuàng)建數(shù)據(jù)庫:
create database if not exists ma_ex
comment 'manager_external'
with dbproperties ('name'='dachun','date'='20200202');
我的另一篇博客詳細(xì)介紹了Hive的數(shù)據(jù)庫操作
2,內(nèi)部表演示
創(chuàng)建內(nèi)部表(MANAGED _TABLE):創(chuàng)建內(nèi)部表也可以指定location,內(nèi)部表的數(shù)據(jù)文件就會(huì)存儲(chǔ)在指定路徑下,否則走默認(rèn)的當(dāng)前庫/表名
create table if not exists one(
id int,
name string
) row format delimited fields termited by ',';
內(nèi)部表insert方式插入數(shù)據(jù),生產(chǎn)千萬不要用
因?yàn)樗敲坎迦胍粭l數(shù)據(jù),就會(huì)在該表目錄下生成一個(gè)小文件,每一個(gè)小文件對(duì)應(yīng)一條數(shù)據(jù),10W條對(duì)應(yīng)10W個(gè)小文件
insert into one values(1,'dema');
insert into one values(2,'xiya');
小文件
內(nèi)部表load方式插入數(shù)據(jù)
load data local inpath '/home/hadoopadmin/one.txt' into table one
load方式直接把這個(gè)文件拷貝到了目錄下.
或者不用這個(gè)load命令,直接再拷貝一個(gè)文件到表one目錄下,執(zhí)行select* from one發(fā)現(xiàn)數(shù)據(jù)也加載進(jìn)來了
hdfs dfs -put /home/hadoopadmin/one.txt
/user/hive/warehouse/ma_ex.db/one/one2.txt
直接上傳文件方式
在MySQL的元數(shù)據(jù)表TBLS里面查看
select * from TBLS;
字段TBL_TYPE=MANAGED_TABLE表示是內(nèi)部表
image.png
3,外部表不帶location演示
創(chuàng)建外部表
create external table if not exists one_external(
id int,
name string
) row format delimited fields terminated by ',';
如果創(chuàng)建外部表時(shí)候末尾沒有加上location,
他默認(rèn)讀取的是自己database/自己tablename/文件
通過hdfs上傳文件方式往這個(gè)目錄下上傳一個(gè)文件
hdfs dfs -put /home/hadoopadmin/one.txt /user/hive/warehouse/ma_ex.db/one_external
通過load方式網(wǎng)這個(gè)表里面添加數(shù)據(jù)
load data local inpath '/home/hadoopadmin/one2.txt' into table one_external;
外部表同樣也可以通過insert方式添加數(shù)據(jù),同樣對(duì)應(yīng)目錄下新增一個(gè)文件
insert into one_external values(1,'lala');
上面三中添加方式都可以在外部表添加數(shù)據(jù)
查看元數(shù)據(jù)TABLS
image.png
4,外部表帶location演示
創(chuàng)建外部表時(shí)候指定新的location
首先將文件導(dǎo)入到外部表的目錄下
hdfs dfs -put /home/hadoopadmin/one.txt /user/hive/warehouse/external/
create external table if not exists one_external2(
id int,
name string
) row format delimited fields terminated by ','
location '/user/hive/warehouse/external/'
因?yàn)槲覀冊(cè)谕獠勘淼穆窂较乱呀?jīng)導(dǎo)入了文件
直接執(zhí)行select * from one_external2是能夠看到數(shù)據(jù)的
hive (ma_ex)> select * from one_external2;
OK
3 shuruima
4 heisemeigui
5 niuqu
在元數(shù)據(jù)存儲(chǔ)的表的表TBLS也能看到
image.png
但是在hdfs上面并不能能夠看到相關(guān)的目錄,在該表數(shù)據(jù)庫文件夾的下關(guān)于external2的目錄也無法找到,通過下面這個(gè)desc formatted 表名,能夠知道這個(gè)外部表的路徑定在了外部表建表語句的location的文件夾下
image.png
為什么可以確認(rèn)外部表的文件夾自動(dòng)定位到了location文件夾下呢?
1,通過下面的desc table formatted確定了
2,insert into 表 values ();執(zhí)行完之后,發(fā)現(xiàn)這個(gè)外部表的location文件夾下多出來一個(gè)文件,這個(gè)文件就是我們插入的這一條數(shù)據(jù),所以呢,這就定位到了,創(chuàng)建帶location的外部表對(duì)應(yīng)的表數(shù)據(jù)存儲(chǔ)路徑就默認(rèn)使用這個(gè)location位置
5,desc formatted 表名獲取信息
使用desc formatted 表名,截圖關(guān)鍵點(diǎn)
desc formatted one;
one
desc formatted one_external;
one_external
desc formatted one_external2;
one_external2
我們會(huì)發(fā)現(xiàn)指定location的外部表,這個(gè)location路徑就是它對(duì)應(yīng)的文件的路徑,他并沒有自己創(chuàng)建相關(guān)路徑,但是創(chuàng)建內(nèi)部表和沒有指定路徑的外部表他是在數(shù)據(jù)庫的hdfs路徑下真實(shí)創(chuàng)建了這個(gè)文件夾.下面以刪除表為例測(cè)試.
6,總結(jié)
總結(jié):內(nèi)部表
內(nèi)部表創(chuàng)建時(shí)候,會(huì)在hdfs文件系統(tǒng)上面創(chuàng)建相應(yīng)的路徑文件夾,相關(guān)表的數(shù)據(jù)都會(huì)存在里面,如果是insert into 進(jìn)去的數(shù)據(jù),每一條都會(huì)對(duì)應(yīng)一個(gè)小文件,可以把文件已load方式加載進(jìn)去或者直接把文件放到hdfs文件系統(tǒng)對(duì)應(yīng)的路徑里面,前提是創(chuàng)建表的時(shí)候,制定好分隔符,然后執(zhí)行select * from 表,就可以將文件里面的數(shù)據(jù)讀到表中
刪除內(nèi)部表(MANAGED_TABLE)的時(shí)候,在 MySQL的元數(shù)據(jù)表TBLS里面對(duì)應(yīng)的one表內(nèi)容被刪除,另外HDFS文件系統(tǒng)上面的該表的數(shù)據(jù)都會(huì)被刪除
總結(jié):外部表沒有指定location.
外部表沒有指定location版本創(chuàng)建時(shí)候,會(huì)在hdfs文件系統(tǒng)上面創(chuàng)建相應(yīng)的路徑文件夾,相關(guān)表的數(shù)據(jù)都會(huì)存在里面,如果是insert into 進(jìn)去的數(shù)據(jù),每一條都會(huì)對(duì)應(yīng)一個(gè)小文件,可以把文件已load方式加載進(jìn)去或者直接把文件放到hdfs文件系統(tǒng)對(duì)應(yīng)的路徑里面,前提是創(chuàng)建表的時(shí)候,制定好分隔符,然后執(zhí)行select * from 表,就可以將文件里面的數(shù)據(jù)讀到表中
刪除時(shí)候,對(duì)應(yīng)的hdfs上面的表路徑以及數(shù)據(jù)都不會(huì)刪除,即使沒指定location,自己創(chuàng)建路徑的話,還是沒有刪除.但是在 MySQL的元數(shù)據(jù)表TBLS里面對(duì)應(yīng)的表內(nèi)容被刪除.
總結(jié):外部表指定location.
創(chuàng)建時(shí)候,如果指定了location的話,該外部表對(duì)應(yīng)的存儲(chǔ)路徑文件夾=location指定的文件夾位置,通過desc formatted tablename和insert into方式插入一條數(shù)據(jù)會(huì)多一個(gè)文件在location文件夾下,得到了驗(yàn)證
刪除時(shí)候,對(duì)應(yīng)的hdfs上面的表路徑以及數(shù)據(jù)都不會(huì)刪除,即使沒指定location,自己創(chuàng)建路徑的話,還是沒有刪除.但是在 MySQL的元數(shù)據(jù)表TBLS里面對(duì)應(yīng)的表內(nèi)容被刪除.
7,內(nèi)部表和外部表和外部表-location三者異同點(diǎn)
內(nèi)部表和外部表的共同點(diǎn):
指定好了分隔符的情況下,直接往他們相關(guān)的目錄下不管是load文件,還是直接hdfs命令上傳文件到這個(gè)路徑,都可以被識(shí)別為表數(shù)據(jù),而且都會(huì)在MySQL元數(shù)據(jù)TBLS表里面存儲(chǔ),刪除表的時(shí)候?qū)?yīng)的TBLS元數(shù)據(jù)都被刪除
另外在hdfs文件系統(tǒng)的a數(shù)據(jù)庫文件夾下都有一個(gè)one的文件夾,如果a數(shù)據(jù)庫下創(chuàng)建一個(gè)內(nèi)部表one,或者創(chuàng)建一個(gè)外部表one指定location為one這個(gè)路徑,或者創(chuàng)建外部表one不帶location,他們?nèi)齻€(gè)讀出來的數(shù)據(jù)一樣.
內(nèi)部表和外部表不同點(diǎn):
內(nèi)部表會(huì)把hdfs表相關(guān)的文件文件夾全干掉,外部表就不會(huì)
外部表指定location和沒指定location異同點(diǎn):
指定location的話該表的數(shù)據(jù)都存儲(chǔ)在location下,沒指定location的話,數(shù)據(jù)都存儲(chǔ)在當(dāng)前數(shù)據(jù)庫文件夾下一個(gè)名字為該表名的文件夾下,刪除表時(shí)候hdfs上面數(shù)據(jù)都不會(huì)被刪除,但是MySQL的元數(shù)據(jù)庫表TBLS數(shù)據(jù)都會(huì)被刪除.
Hive官方介紹
https://cwiki.apache.org/confluence/display/Hive/Managed+vs.+External+Tables