概述
包含External 的表叫外部表
刪除外部表只刪除metastore的元數據,不刪除hdfs中的表數據
外部表 只有一個過程,加載數據和創建表同時完成,并不會移動到數據倉庫目錄中,只是與外部數據建立一個鏈接。當刪除一個 外部表 時,僅刪除該鏈接
指向已經在 HDFS 中存在的數據,可以創建 Partition
它和 內部表 在元數據的組織上是相同的,而實際數據的存儲則有較大的差異
語法
CREATE EXTERNAL TABLE page_view
( viewTime INT,?
? userid BIGINT,
? page_url STRING,
?referrer_url STRING,
? ip STRING COMMENT 'IP Address of the User',
? country STRING COMMENT 'country of origination‘
)
? ? COMMENT 'This is the staging page view table'
? ? ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'
? ? STORED AS TEXTFILE
? ? LOCATION 'hdfs://centos:9000/user/data/staging/page_view';
實驗
hive (default)> create external table
? ? ? ? ? ? ? > external_test(name string,city string,address string)
? ? ? ? ? ? ? > row format delimited fields terminated by '\t'
? ? ? ? ? ? ? > location '/home/hive/extable';
OK
Time taken: 0.559 seconds
hive (default)> desc formatted external_test;
OK
col_name ? ? ? ?data_type ? ? ? comment
# col_name ? ? ? ? ? ? ?data_type ? ? ? ? ? ? ? comment ? ? ? ? ? ??
? ? ? ? ? ? ? ? ?
name ? ? ? ? ? ? ? ? ? ?string ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
city ? ? ? ? ? ? ? ? ? ?string ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
address ? ? ? ? ? ? ? ? string ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ?
# Detailed Table Information ? ? ? ? ? ??
Database: ? ? ? ? ? ? ? default ? ? ? ? ? ? ? ? ?
Owner: ? ? ? ? ? ? ? ? ?hadoop ? ? ? ? ? ? ? ? ??
CreateTime: ? ? ? ? ? ? Wed Sep 21 20:18:21 CST 2016 ? ??
LastAccessTime: ? ? ? ? UNKNOWN ? ? ? ? ? ? ? ? ?
Retention: ? ? ? ? ? ? ?0 ? ? ? ? ? ? ? ? ? ? ? ?
Location: ? ? ? ? ? ? ? hdfs://hello110:9000/home/hive/extable ??
Table Type: ? ? ? ? ? ? EXTERNAL_TABLE ?? ? ? ??
Table Parameters: ? ? ? ? ? ? ? ?
? ? ? ? EXTERNAL ? ? ? ? ? ? ? ?TRUE ? ? ? ? ? ? ? ?
? ? ? ? transient_lastDdlTime ? 1474460301 ? ? ? ? ?
? ? ? ? ? ? ? ? ?
# Storage Information ? ? ? ? ? ?
SerDe Library: ? ? ? ? ?org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe ? ? ??
InputFormat: ? ? ? ? ? ?org.apache.hadoop.mapred.TextInputFormat ? ? ? ??
OutputFormat: ? ? ? ? ? org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat ? ? ??
Compressed: ? ? ? ? ? ? No ? ? ? ? ? ? ? ? ? ? ??
Num Buckets: ? ? ? ? ? ?-1 ? ? ? ? ? ? ? ? ? ? ??
Bucket Columns: ? ? ? ? [] ? ? ? ? ? ? ? ? ? ? ??
Sort Columns: ? ? ? ? ? [] ? ? ? ? ? ? ? ? ? ? ??
Storage Desc Params: ? ? ? ? ? ??
? ? ? ? field.delim ? ? ? ? ? ? \t ? ? ? ? ? ? ? ? ?
? ? ? ? serialization.format ? ?\t ? ? ? ? ? ? ? ? ?
Time taken: 0.065 seconds, Fetched: 29 row(s)
hive (default)> load data local inpath '/data/ext_test' into table external_test;
Loading data to table default.external_test
OK
Time taken: 1.286 seconds
hive (default)> select * from external_test;
OK
external_test.name ? ? ?external_test.city ? ? ?external_test.address
1 ? ? ? dddd ? ?dddd
2 ? ? ? www ? ? www
3 ? ? ? eeee ? ?wwww
4 ? ? ? tttt ? ?cccc
5 ? ? ? yyycc ? dddd
Time taken: 1.92 seconds, Fetched: 5 row(s)
不指定location,會默認在:hdfs://hello110:9000/user/hive/warehouse/ 下面
刪除
外部表drop table t1。hdfs里數據還在,不會刪除。
再創建與剛才相同的表名的表的時候,
select * from t1,會發現表數據會依然還在。
(如果指定了location,則location要一樣,如果沒有指定,則都在?/user/hive/warehouse 下。)