1. 程式人生 > >Hive新增刪除修改欄位

Hive新增刪除修改欄位

新增刪除欄位

語法:

ALTER TABLE table_name 
  [PARTITION partition_spec]                 -- (Note: Hive 0.14.0 and later)
  ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...)
  [CASCADE|RESTRICT]                         -- (Note: Hive 1.1.0 and later)

示例:

// 建立測試表
CREATE TABLE IF NOT EXISTS test (id BIGINT, name STRING);
// 新增欄位
ALTER TABLE test ADD COLUMNS(t_1 STRING);
// 刪除欄位(使用新schema替換原有的)
ALTER TABLE test REPLACE COLUMNS(id BIGINT, name STRING);

修改欄位(名稱/型別/位置/註釋)

語法:

ALTER TABLE table_name [PARTITION partition_spec] CHANGE [COLUMN] col_old_name col_new_name column_type
  [COMMENT col_comment] [FIRST|AFTER column_name] [CASCADE|RESTRICT];

示例:

CREATE TABLE test_change (a int, b int, c int);
 
// First change column a's name to a1.
ALTER TABLE test_change CHANGE a a1 INT;
 
// Next change column a1's name to a2, its data type to string, and put it after column b.
ALTER TABLE test_change CHANGE a1 a2 STRING AFTER b;
// The new table's structure is:  b int, a2 string, c int.
  
// Then change column c's name to c1, and put it as the first column.
ALTER TABLE test_change CHANGE c c1 INT FIRST;
// The new table's structure is:  c1 int, b int, a2 string.
  
// Add a comment to column a1
ALTER TABLE test_change CHANGE a1 a1 INT COMMENT 'this is column a1';