hive創建表的三種方式

hive的表分爲三種:內部表(刪除表會同時刪除HDFS文件)、外部表(刪除表只會刪除源數據,並不會刪除HDFS文件)、臨時表(只會當前會話有效,會話結束,臨時表消失)
hive創建表的方式有三種:

  1. 直接創建表
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name    -- (Note: TEMPORARY available in Hive 0.14.0 and later)
  [(col_name data_type [column_constraint_specification] [COMMENT col_comment], ... [constraint_specification])]
  [COMMENT table_comment]
  [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
  [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
  [SKEWED BY (col_name, col_name, ...)                  -- (Note: Available in Hive 0.10.0 and later)]
     ON ((col_value, col_value, ...), (col_value, col_value, ...), ...)
     [STORED AS DIRECTORIES]
  [
   [ROW FORMAT row_format] 
   [STORED AS file_format]
     | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)]  -- (Note: Available in Hive 0.6.0 and later)
  ]
  [LOCATION hdfs_path]
  [TBLPROPERTIES (property_name=property_value, ...)]   -- (Note: Available in Hive 0.6.0 and later)
  [AS select_statement];   -- (Note: Available in Hive 0.5.0 and later; not supported for external tables)

以上爲官網語句,解釋下:
[]中的爲可選項
TEMPORARY:爲臨時表的關鍵字,只對當前會話有效,會話結束,臨時表消失
EXTERNAL:外部表關鍵字,刪除外部表只會刪除元數據,並不會刪除對應的HDFS文件
如果以上兩個關鍵字都沒選(只能選一個關鍵字),默認爲內部表,刪除表不僅會刪除元數據,也會刪除對應的HDFS文件
column_constraint_specification:列的約束,類似於MySQL的列約束
COMMENT:字段和表的描述
PARTITIONED BY:分區
CLUSTERED BY SORTED BY:分桶
SKEWED BY:指定傾斜字段及值
ROW FORMAT:指定行的分隔符
STORED AS :指定存儲格式
LOCATION :指定HDFS文件位置
TBLPROPERTIES :指定配置屬性

create table temp.temp_create_table_test (
col1   string  comment "字段1",
col2   int     comment "字段2"
)
comment "測試"
partitioned by (part_col string comment "分區字段")
clustered by (col1) sorted by (col1 desc) into 2 buckets 
skewed by (col1) on ((2), (3))
row format delimited fields terminated by "\t"
stored as parquet 
location "/user/hive/warehouse-3.1.1/temp.db/temp_create_table_test"
tblproperties("parquet.compression"="snappy");
  1. 通過複製表結構創建新表
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name
  LIKE existing_table_or_view_name
  [LOCATION hdfs_path];
create table temp.temp_create_table_like_test like temp.temp_create_table_test;
  1. 通過子查詢創建表
create  table temp.temp_create_table_as_test as
select col1, col2 from temp.temp_create_table_test where part_col = '2';

官網地址:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章