問題導讀:
1.建立內部表與外部表的區別是什麼?
2.external關鍵字的作用是什麼?
3.外部表與內部表的區別是什麼?
4.刪除表的時候,內部表與外部表有什麼區別?
5.load data local inpath '/home/wyp/data/wyp.txt' into table wyp;的過程是什麼樣子的?
6.磁碟,hdfs,hive表他們之間的過程是什麼樣子的?
好了,進入正題。今天我們要**的話題是hive的裡面的表與外部表兩個概念,以及如何在hive裡面建立表和外部表,它們之間有什麼區別等話題。在本部落格的 讓你徹底明白hive資料儲存各種模式文章裡面我們談到了hive的資料儲存模式,裡面簡單的說到hive中表以及外部表的簡單概念,相信很多讀者對這些概念還不是很了解,今天就給大家科普一下,希望對大家有所幫助。
相信很多使用者都用過關係型資料庫,我們可以在關係型資料庫裡面建立表(create table),這裡要討論的表和關係型資料庫中的表在概念上很類似。我們可以用下面的語句在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 複製** 這樣我們就在hive裡面建立了一張普通的表,現在我們給這個表匯入資料:
hive> load data local inpath '/home/wyp/data/wyp.txt' into table wyp;
copying data from file:/home/wyp/data/wyp.txt
copying file: file:/home/hdfs/wyp.txt
loading data to table default.wyp
table default.wyp stats: [num_partitions: 0, num_files: 1,
num_rows: 0, total_size: 67, raw_data_size: 0]
oktime taken: 3.289 seconds
hive> select * from wyp;
ok1 wyp 25 13188888888888
2 test 30 13888888888888
3 zs 34 899314121
time taken: 0.41 seconds, fetched: 3 row(s) 複製**
注意:/home/wyp/data/路徑是linux本地檔案系統路徑;而/home/hdfs/是hdfs檔案系統上面的路徑!從上面的輸出我們可以看到資料是先從本地的/home/wyp/data/資料夾下複製到hdfs上的/home/hdfs/wyp.txt(這個是hive中的配置導致的)檔案中!最後hive將從hdfs上把資料移動到wyp表中!移到表中的資料到底存放在hdfs的什麼地方?其實在hive的$/conf/hive-site.xml配置檔案的hive.metastore.warehouse.dir屬性指向的就是hive表資料存放的路徑(在我的店電腦裡面配置是/user/hive/warehouse),而hive每建立乙個表都會在hive.metastore.warehouse.dir指向的目錄下以表名建立乙個資料夾,所有屬於這個表的資料都存放在這個資料夾裡面。所以,剛剛匯入到wyp表的資料都存放在/user/hive/warehouse/wyp/資料夾中,我們可以去看看:
hive> dfs -ls /user/hive/warehouse/wyp ;
found 1 items
-rw-r--r-- 3 wyp supergroup 67 2014-01-14 22:23 /user/hive/warehouse/wyp/wyp.txt 複製**
看到沒,上面的命令就是顯示hdfs上的/user/hive/warehouse/wyp中的所有內容。如果需要刪除wyp表,可以用下面的命令:
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 複製**
從上面的輸出moved: 『hdfs://mycluster/user/hive/warehouse/wyp』 to trash at: hdfs://mycluster/user/hdfs/.trash/current我們可以得知,原來屬於wyp表的資料被移到hdfs://mycluster/user/hdfs/.trash/current資料夾中(如果你的hadoop沒有取用垃圾箱機制,那麼drop table wyp命令將會把屬於wyp表的所有資料全部刪除!),其實就是刪掉了屬於wyp表的資料。記住這些,因為這些和外部表有很大的不同。同時,屬於表wyp的元資料也全部刪除了!
我們再來建立乙個外部表:
hive> create external table exter_table(
> id int,
> name string,
> age int,
> tel string)
> location '/home/wyp/external';
oktime taken: 0.098 seconds 複製**
仔細觀察一下建立表和外部表的區別,仔細的同學們乙個會發現建立外部表多了external關鍵字說明以及location 『/home/wyp/external』。是的,你說對了!如果你需要建立外部表,需要在建立表的時候加上external關鍵字,同時指定外部表存放資料的路徑(當然,你也可以不指定外部表的存放路徑,這樣hive將在hdfs上的/user/hive/warehouse/資料夾下以外部表的表名建立乙個資料夾,並將屬於這個表的資料存放在這裡):
hive> load data local inpath '/home/wyp/data/wyp.txt' into table exter_table;
copying data from file:/home/wyp/data/wyp.txt
copying file: file:/home/hdfs/wyp.txt
loading data to table default.exter_table
table default.exter_table stats: [num_partitions: 0, num_files:
1, num_rows: 0, total_size: 67, raw_data_size: 0]
oktime taken: 0.456 seconds 複製**
和上面的匯入資料到表一樣,將本地的資料匯入到外部表,資料也是從本地檔案系統複製到hdfs中/home/hdfs/wyp.txt檔案中,但是,最後資料不是移動到外部表的/user/hive/warehouse/exter_table資料夾中(除非你建立表的時候沒有指定資料的存放路徑)!大家可以去hdfs上看看!對於外部表,資料是被移動到建立表時指定的目錄(本例是存放在/home/wyp/external資料夾中)!如果你要刪除外部表,看看下面的操作:
hive> drop table exter_table;ok
time taken: 0.093 seconds 複製** 和上面刪除hive的表對比可以發現,沒有輸出將資料從乙個地方移到任乙個地方!那是不是刪除外部表的的時候資料直接被刪除掉呢?答案不是這樣的:
hive> dfs -ls /home/wyp/external;
found 1 items
-rw-r--r-- 3 wyp supergroup 67 2014-01-14 23:21 /home/wyp/external/wyp.txt 複製**
你會發現刪除外部表的時候,資料並沒有被刪除,這是和刪除表的資料完全不一樣的!
最後歸納一下hive中表與外部表的區別:
1、在匯入資料到外部表,資料並沒有移動到自己的資料倉儲目錄下,也就是說外部表中的資料並不是由它自己來管理的!而表則不一樣;
2、在刪除表的時候,hive將會把屬於表的元資料和資料全部刪掉;而刪除外部表的時候,hive僅僅刪除外部表的元資料,資料是不會刪除的!
那麼,應該如何選擇使用哪種表呢?在大多數情況沒有太多的區別,因此選擇只是個人喜好的問題。但是作為乙個經驗,如果所有處理都需要由hive完成,那麼你應該建立表,否則使用外部表!
Hive內部表 外部表區別
hive內部表 外部表區別自不用說,可實際用的時候還是要小心。1.內部表 sql view plain copy print create table tt name string age string location input table data 此時,會在hdfs上新建乙個tt表的資料存放...
Hive內部表 外部表 區別
以下基於hive 2.0.0 snapshot,本人親自實驗的結論!新增欄位會導致表結構同時改變,與是否是外部表內部表無關,也與是否指定location無關!從本地load新分割槽的資料會導致表的分割槽資訊同時改變,與是否是外部表內部表無關,也與是否指定location無關!2 drop表時,如果是...
hive 內部表與外部表的區別
本文以例子的形式介紹一下hive內錶和外表的區別。例子共有4個 不帶分割槽的內錶 帶分割槽的內錶 不帶分割槽的外表 帶分割槽的外表。1 不帶分割槽的內錶 建立表 create table innertable id int,name string row format delimited field...