今天我們要探討的話題是Hive的里面的表與外部表兩個概念,以及如何在Hive里面創(chuàng)建表和外部表,它們之間有什么區(qū)別等話題。
相信很多用戶都用過關(guān)系型數(shù)據(jù)庫,我們可以在關(guān)系型數(shù)據(jù)庫里面創(chuàng)建表(create table),這里要討論的表和關(guān)系型數(shù)據(jù)庫中的表在概念上很類似。我們可以用下面的語句在Hive里面創(chuàng)建一個表:
1. hive> create table wyp(id int,
2. > name string,
3. > age int,
4. > tele string)
5. > ROW FORMAT DELIMITED
6. > FIELDS TERMINATED BY '\t'
7. > STORED AS TEXTFILE;
8. OK
9. Time taken: 0.759 seconds
這樣我們就在Hive里面創(chuàng)建了一張普通的表,現(xiàn)在我們給這個表導入數(shù)據(jù):
1. hive> load data local inpath '/home/wyp/data/wyp.txt' into table wyp;
2. Copying data from file:/home/wyp/data/wyp.txt
3. Copying file: file:/home/hdfs/wyp.txt
4. Loading data to table default.wyp
5. Table default.wyp stats: [num_partitions: 0, num_files: 1,
6. num_rows: 0, total_size: 67, raw_data_size: 0]
7. OK
8. Time taken: 3.289 seconds
9. hive> select * from wyp;
10. OK
11. 1 wyp 25 13188888888888
12. 2 test 30 13888888888888
13. 3 zs 34 899314121
14. Time taken: 0.41 seconds, Fetched: 3 row(s)
注意:
/home/wyp/data/路徑是Linux本地文件系統(tǒng)路徑;而/home/hdfs/是HDFS文件系統(tǒng)上面的路徑!從上面的輸出我們可以看到數(shù)據(jù)是先從本地的/home/wyp/data/文件夾下復制到HDFS上的/home/hdfs/wyp.txt(這個是Hive中的配置導致的)文件中!最后Hive將從HDFS上把數(shù)據(jù)移動到wyp表中!移到表中的數(shù)據(jù)到底存放在HDFS的什么地方?其實在Hive的${HIVE_HOME}/conf/hive-site.xml配置文件的hive.metastore.warehouse.dir屬性指向的就是Hive表數(shù)據(jù)存放的路徑(在我的店電腦里面配置是/user/hive/warehouse),而Hive每創(chuàng)建一個表都會在hive.metastore.warehouse.dir指向的目錄下以表名創(chuàng)建一個文件夾,所有屬于這個表的數(shù)據(jù)都存放在這個文件夾里面。所以,剛剛導入到wyp表的數(shù)據(jù)都存放在/user/hive/warehouse/wyp/文件夾中,我們可以去看看:
1. hive> dfs -ls /user/hive/warehouse/wyp ;
2. Found 1 items
3. -rw-r--r-- 3 wyp supergroup 67 2014-01-14 22:23 /user/hive/warehouse/wyp/wyp.txt
看到?jīng)],上面的命令就是顯示HDFS上的/user/hive/warehouse/wyp中的所有內(nèi)容。如果需要刪除wyp表,可以用下面的命令:
1. hive> drop table wyp;
2. Moved: 'hdfs://mycluster/user/hive/warehouse/wyp' to
3. trash at: hdfs://mycluster/user/hdfs/.Trash/Current
4. OK
5. Time taken: 2.503 seconds
從上面的輸出Moved: ‘hdfs://mycluster/user/hive/warehouse/wyp’ to trash at: hdfs://mycluster/user/hdfs/.Trash/Current我們可以得知,原來屬于wyp表的數(shù)據(jù)被移到hdfs://mycluster/user/hdfs/.Trash/Current文件夾中(如果你的Hadoop沒有取用垃圾箱機制,那么drop table wyp命令將會把屬于wyp表的所有數(shù)據(jù)全部刪除?。鋵嵕褪莿h掉了屬于wyp表的數(shù)據(jù)。記住這些,因為這些和外部表有很大的不同。同時,屬于表wyp的元數(shù)據(jù)也全部刪除了!
我們再來創(chuàng)建一個外部表:
1. hive> create external table exter_table(
2. > id int,
3. > name string,
4. > age int,
5. > tel string)
6. > location '/home/wyp/external';
7. OK
8. Time taken: 0.098 seconds
仔細觀察一下創(chuàng)建表和外部表的區(qū)別,仔細的同學們一個會發(fā)現(xiàn)創(chuàng)建外部表多了external關(guān)鍵字說明以及l(fā)ocation ‘/home/wyp/external’。是的,你說對了!如果你需要創(chuàng)建外部表,需要在創(chuàng)建表的時候加上external關(guān)鍵字,同時指定外部表存放數(shù)據(jù)的路徑(當然,你也可以不指定外部表的存放路徑,這樣Hive將在HDFS上的/user/hive/warehouse/文件夾下以外部表的表名創(chuàng)建一個文件夾,并將屬于這個表的數(shù)據(jù)存放在這里):
1. hive> load data local inpath '/home/wyp/data/wyp.txt' into table exter_table;
2. Copying data from file:/home/wyp/data/wyp.txt
3. Copying file: file:/home/hdfs/wyp.txt
4. Loading data to table default.exter_table
5. Table default.exter_table stats: [num_partitions: 0, num_files:
6. 1, num_rows: 0, total_size: 67, raw_data_size: 0]
7. OK
8. Time taken: 0.456 seconds
和上面的導入數(shù)據(jù)到表一樣,將本地的數(shù)據(jù)導入到外部表,數(shù)據(jù)也是從本地文件系統(tǒng)復制到HDFS中/home/hdfs/wyp.txt文件中,但是,最后數(shù)據(jù)不是移動到外部表的/user/hive/warehouse/exter_table文件夾中(除非你創(chuàng)建表的時候沒有指定數(shù)據(jù)的存放路徑)!大家可以去HDFS上看看!對于外部表,數(shù)據(jù)是被移動到創(chuàng)建表時指定的目錄(本例是存放在/home/wyp/external文件夾中)!如果你要刪除外部表,看看下面的操作:
1. hive> drop table exter_table;
2. OK
3. Time taken: 0.093 seconds
和上面刪除Hive的表對比可以發(fā)現(xiàn),沒有輸出將數(shù)據(jù)從一個地方移到任一個地方!那是不是刪除外部表的的時候數(shù)據(jù)直接被刪除掉呢?答案不是這樣的:
1. hive> dfs -ls /home/wyp/external;
2. Found 1 items
3. -rw-r--r-- 3 wyp supergroup 67 2014-01-14 23:21 /home/wyp/external/wyp.txt
你會發(fā)現(xiàn)刪除外部表的時候,數(shù)據(jù)并沒有被刪除,這是和刪除表的數(shù)據(jù)完全不一樣的!
最后歸納一下Hive中表與外部表的區(qū)別:
1、在導入數(shù)據(jù)到外部表,數(shù)據(jù)并沒有移動到自己的數(shù)據(jù)倉庫目錄下,也就是說外部表中的數(shù)據(jù)并不是由它自己來管理的!而表則不一樣;
2、在刪除表的時候,Hive將會把屬于表的元數(shù)據(jù)和數(shù)據(jù)全部刪掉;而刪除外部表的時候,Hive僅僅刪除外部表的元數(shù)據(jù),數(shù)據(jù)是不會刪除的!
那么,應該如何選擇使用哪種表呢?在大多數(shù)情況沒有太多的區(qū)別,因此選擇只是個人喜好的問題。但是作為一個經(jīng)驗,如果所有處理都需要由Hive完成,那么你應該創(chuàng)建表,否則使用外部表!