Hive 修改 查詢 分區信息 列 及 表 語句 整理

1. 分區操作

1.1 查詢分區信息

show partitions t_test;

describe formatted t_test partition (sample_date="20190723", partition_name="7");

show table extended like 't_test' partition (sample_date="20190723",partition_name="7");

show partitions t_test PARTITION(sample_date=20190826, partition_name=9); 

1.2 添加分區

ALTER TABLE table_name ADD PARTITION (partCol = 'value1') location 'loc1'; //示例

ALTER TABLE table_name ADD IF NOT EXISTS PARTITION (dt='20130101') LOCATION '/user/hadoop/warehouse/table_name/dt=20130101'; //一次添加一個分區

ALTER TABLE page_view ADD PARTITION (dt='2008-08-08', country='us') location '/path/to/us/part080808' PARTITION (dt='2008-08-09', country='us') location '/path/to/us/part080809';  //一次添加多個分區
 

1.3 刪除分區

ALTER TABLE login DROP IF EXISTS PARTITION (dt='2008-08-08');

ALTER TABLE page_view DROP IF EXISTS PARTITION (dt='2008-08-08', country='us');

1.4 修改分區

ALTER TABLE table_name PARTITION (dt='2008-08-08') SET LOCATION "new location";

ALTER TABLE table_name PARTITION (dt='2008-08-08') RENAME TO PARTITION (dt='20080808');

2.列操作

2.1 添加列

ALTER TABLE table_name ADD COLUMNS (col_name STRING);  //在所有存在的列後面,但是在分區列之前添加一列

2.2 修改列

CREATE TABLE test_change (a int, b int, c int);

// will change column a's name to a1
ALTER TABLE test_change CHANGE a a1 INT; 

// will change column a's name to a1, a's data type to string, and put it after column b. The new table's structure is: b int, a1 string, c int
ALTER TABLE test_change CHANGE a a1 STRING AFTER b; 

// will change column b's name to b1, and put it as the first column. The new table's structure is: b1 int, a string, c int
ALTER TABLE test_change CHANGE b b1 INT FIRST; 

3. 表操作

3.1 創建外部表

CREATE EXTERNAL TABLE `t_test`(      
   `row_key` string,                                  
   `type` string,
   `time` string,                               
   `id` string)                            
 PARTITIONED BY (
   `date` string,
   `partition` string) 

3.2 修改表屬性

alter table table_name set TBLPROPERTIES ('EXTERNAL'='TRUE');  //內部錶轉外部表 

alter table table_name set TBLPROPERTIES ('EXTERNAL'='FALSE');  //外部錶轉內部表

3.3 表的重命名

ALTER TABLE table_name RENAME TO new_table_name

4. Ref

  1. https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-Add%2FReplaceColumns
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章