一、創建表
1.語法
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment], …)] [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] [ROW FORMAT row_format] [STORED AS file_format] [LOCATION hdfs_path]
2.解釋
(1)CREATE TABLE 創建一個指定名字的表。如果相同名字的表已經存在,則拋出異常;用戶可以用 IF NOT EXISTS 選項來忽略這個異常。
(2)EXTERNAL關鍵字可以讓用戶創建一個外部表,在建表的同時指定一個指向實際數據的路徑(LOCATION),Hive創建內部表時,會將數據移動到數據倉庫指向的路徑;若創建外部表,僅記錄數據所在的路徑,不對數據的位置做任何改變。在刪除表的時候,內部表的元數據和數據會被一起刪除,而外部表只刪除元數據,不刪除數據。
(3)COMMENT:為表和列添加注釋。
(4)PARTITIONED BY創建分區表
(5)CLUSTERED BY創建分桶表
(6)SORTED BY不常用
(7)ROW FORMAT
DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char] [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char] | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, …)]
用戶在建表的時候可以自定義SerDe或者使用自帶的SerDe。如果沒有指定ROW FORMAT 或者ROW FORMAT DELIMITED,將會使用自帶的SerDe。在建表的時候,用戶還需要為表指定列,用戶在指定表的列的同時也會指定自定義的SerDe,Hive通過SerDe確定表的具體的列的數據。
SerDe是Serialize/Deserilize的簡稱,目的是用於序列化和反序列化。
(8)STORED AS指定存儲文件類型
常用的存儲文件類型:SEQUENCEFILE(二進制序列文件)、TEXTFILE(文本)、RCFILE(列式存儲格式文件)
如果文件數據是純文本,可以使用STORED AS TEXTFILE。如果數據需要壓縮,使用 STORED AS SEQUENCEFILE。
(9)LOCATION :指定表在HDFS上的存儲位置。
(10)LIKE允許用戶復制現有的表結構,但是不復制數據。
3.例子
詳見管理表和外部表章節
二、修改表
2.1.重命名表
1.語法
ALTER TABLE table_name RENAME TO new_table_name
2.實操案例
hive (default)> alter table dept_partition2 rename to dept_partition3;
OK
Time taken: 0.452 seconds
2.2 增加、刪除、修改列信息
1.語法
更新列
ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]
增加和替換列
ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], …)
注:ADD是代表新增一字段,字段位置在所有列后面(partition列前),REPLACE則是表示替換表中所有字段。
2.實操案例
(1)查詢表結構及數據hive (default)> desc dept_partition;
OK col_name data_type comment deptno int dname string loc string month string # Partition Information # col_name data_type comment month string Time taken: 0.118 seconds, Fetched: 9 row(s) hive (default)> select * from dept_partition; OK dept_partition.deptno dept_partition.dname dept_partition.loc dept_partition.month 10 CCOUNTING 1700 202009 20 RESEARCH 1800 202009 30 SALE 1900 202009 40 OPERATIONS 1700 202009 10 CCOUNTING 1700 202010 20 RESEARCH 1800 202010 30 SALE 1900 202010 40 OPERATIONS 1700 202010 10 CCOUNTING 1700 202011 20 RESEARCH 1800 202011 30 SALE 1900 202011 40 OPERATIONS 1700 202011 Time taken: 0.114 seconds, Fetched: 12 row(s)
(2)添加列
hive (default)> alter table dept_partition add columns(deptdesc string); OK Time taken: 0.287 seconds
(3)查詢表結構
hive (default)> desc dept_partition; OK col_name data_type comment deptno int dname string loc string deptdesc string month string # Partition Information # col_name data_type comment month string Time taken: 0.089 seconds, Fetched: 10 row(s) hive (default)> select * from dept_partition; OK dept_partition.deptno dept_partition.dname dept_partition.loc dept_partition.deptdesc dept_partition.month 10 CCOUNTING 1700 NULL 202009 20 RESEARCH 1800 NULL 202009 30 SALE 1900 NULL 202009 40 OPERATIONS 1700 NULL 202009 10 CCOUNTING 1700 NULL 202010 20 RESEARCH 1800 NULL 202010 30 SALE 1900 NULL 202010 40 OPERATIONS 1700 NULL 202010 10 CCOUNTING 1700 NULL 202011 20 RESEARCH 1800 NULL 202011 30 SALE 1900 NULL 202011 40 OPERATIONS 1700 NULL 202011 Time taken: 0.125 seconds, Fetched: 12 row(s)
(4)更新列
hive (default)> alter table dept_partition change column deptdesc desc int; OK Time taken: 0.228 seconds
(5)查詢表結構
hive (default)> desc dept_partition; OK col_name data_type comment deptno int dname string loc string desc int month string # Partition Information # col_name data_type comment month string Time taken: 0.096 seconds, Fetched: 10 row(s)
(6)替換列
hive (default)> alter table dept_partition replace columns(deptno string,dname string,loc string); OK Time taken: 0.185 seconds
(7)查詢表結構
hive (default)> desc dept_partition; OK col_name data_type comment deptno string dname string loc string month string # Partition Information # col_name data_type comment month string Time taken: 0.095 seconds, Fetched: 9 row(s)
三、刪除表
hive (default)> drop table dept_partition;
整理源於atguigu視頻