hive中的外表EXTERNAL TABLE

來源:互聯網
上載者:User

在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/’;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.