數據表是存儲數據的基本單位,Hive數據表主要分為內部表(又叫托管表)和外部表,以內部表和外部表為基礎可以創建分區表或分桶表,即內/外部分區表或內/外部分桶表。接下來,針對內部表和外部表進行詳細講解。
默認情況下,內部表和外部表的數據都存儲在Hive配置文件中參數hive.metastore.warehouse.dir指定的路徑。它們的區別在于刪除內部表時,內部表的元數據和數據會一同刪除;而刪除外部表時,只刪除外部表的元數據,不會刪除數據。外部表相對來說更加安全,數據組織更加靈活并且方便共享源數據文件。
CREATE TABLE IF NOT EXISTS
hive_database.managed_table(
staff_id INT COMMENT "This is staffid",
staff_name STRING COMMENT "This is staffname",
salary FLOAT COMMENT "This is staff salary",
hobby ARRAY<STRING> CONMENT "This is staff hobby",
deduct(hps MAP<STRING, FLOAT> COMMENT "This is staff deduction",
address STRUCT<street:STRING,city:STRING> COMMENT "This is staff address"
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY
MAP KEYS TERMINMTED BY ':'
LINES TERMINATED BY '\n'
STORED AS textfile
TBLPROPERTIES("comment"="This is a managed table");
上述命令中,指定ROW FORMAT DELIMITED子句使用Hive內置的Serde,自定義字段(FIELDS)分隔符為“,”;自定義集合元素(COLLECTION ITEMS)的分隔符為“_”;自定義MAP(MAP KEYS)的鍵值對分隔符為“:”;自定義行(LINES)分隔符為\n。
(2)創建外部表external_table的命令如下。
CREATE TABLE IF NOT EXISTS
hive_database.managed_table(
staff_id INT COMMENT "This is staffid",
staff_name STRING COMMENT "This is staffname",
salary FLOAT COMMENT "This is staff salary",
hobby ARRAY<STRING> CONMENT "This is staff hobby",
deduct(hps MAP<STRING, FLOAT> COMMENT "This is staff deduction",
address STRUCT<street:STRING,city:STRING> COMMENT "This is staff address"
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY
MAP KEYS TERMINMTED BY ':'
LINES TERMINATED BY '\n'
STORED AS textfile
TBLPROPERTIES("comment"="This is a managed table");
上述命令中,通過在CREATETABLE句式中指定EXTERNAL子句創建外部表。創建外部表時通常配合LOCATION子句指定數據的存儲位置,便于數據的維護與管理。