在hive中,外表是個很重要的組成部分,通過外表可以很方便進行資料的共用。
因為普通的表會將資料檔案拷貝自己的目錄下,這樣想要分享資料只能儲存多份資料。
但是外表很好的解決了這個問題。
CREATE EXTERNAL TABLE sunwg_test09(id INT, name string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ‘/t’
LOCATION ‘/sunwg/test08′;
上面的語句建立了一張名字為sunwg_test09的外表,該表有id和name兩個欄位,
欄位的分割符為tab,檔案的資料檔案夾為/sunwg/test08
hive> select * from sunwg_test09;
OK
100 tom
101 mary
102 tim
103 kate
104 jone
Time taken: 1.237 seconds
可以查詢到sunwg_test09中的資料。
在目前使用者hive的根目錄下找不到sunwg_test09檔案夾。
此時hive將該表的資料檔案資訊儲存到metadata資料庫中。
mysql> select * from TBLS where TBL_NAME=’sunwg_test09′;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect…
Connection id: 16
Current database: hjl
+——–+————-+——-+——————+——-+———–+——-+—————-+—————-+——————–+——————–+
| TBL_ID | CREATE_TIME | DB_ID | LAST_ACCESS_TIME | OWNER | RETENTION |
SD_ID | TBL_NAME | TBL_TYPE | VIEW_EXPANDED_TEXT |
VIEW_ORIGINAL_TEXT |
+——–+————-+——-+——————+——-+———–+——-+—————-+—————-+——————–+——————–+
| 15 | 1299519817 | 1 | 0 | hjl | 0
| 15 | sunwg_test09 | EXTERNAL_TABLE | NULL |
NULL |
+——–+————-+——-+——————+——-+———–+——-+—————-+—————-+——————–+——————–+
1 row in set (0.01 sec)
可以看到該表的類型為EXTERNAL_TABLE。
mysql> select * from SDS where SD_ID=15;
+——-+——————————————+—————+———————————+————-+————————————————————+———-+
| SD_ID | INPUT_FORMAT | IS_COMPRESSED |
LOCATION | NUM_BUCKETS |
OUTPUT_FORMAT | SERDE_ID |
+——-+——————————————+—————+———————————+————-+————————————————————+———-+
| 15 | org.apache.hadoop.mapred.TextInputFormat | |
hdfs://hadoop00:9000/hjl/test08 | -1 |
org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat | 15 |
+——-+——————————————+—————+———————————+————-+————————————————————+———-+
1 row in set (0.00 sec)
在表SDS中記錄了表sunwg_test09的資料檔案路徑為hdfs://hadoop00:9000/hjl/test08。
實際上外表不光可以指定hdfs的目錄,本地的目錄也是可以的。
比如:
CREATE EXTERNAL TABLE test10(id INT, name string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ‘/t’
LOCATION ‘file:////home/hjl/sunwg/’;