首页 hive修改表结构
文章
取消

hive修改表结构

最近需求改动比较大,需要在 Hive 底层表中增加一些字段,记录一下备用。

Hive 表增加列

分区表增加的列会在分区之前,其它列之后。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
DROP TABLE IF EXISTS test_table;
CREATE TABLE test_table(
  id int
)
;

ALTER TABLE test_table ADD COLUMNS(
    add_column_1 string COMMENT '增加列1',
    add_column_2 string COMMENT '增加列2'
)
;

--------------------------------------------------
hive> DESC test_table;
OK
id                    int
add_column_1          string                增加列1
add_column_2          string                增加列2

Hive 表修改列

1
2
3
4
5
6
7
8
ALTER TABLE test_table CHANGE add_column_2 alter_column_3 string COMMENT '修改列3';

--------------------------------------------------
hive> DESC test_table;
OK
id                    int
add_column_1          string                增加列1
alter_column_3        string                修改列3

Hive 分区表删除列

由于某些原因导致在 Hive 表中增加了一些不需要的列,要是留着的话会出现一些问题(例如插入数据的时候数据列会不同),所以要把这些误加的列“删掉”。在 Hive 中是不允许直接删除表结构的列的,但是可以用一个新的表结构来替换现在的表结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
DROP TABLE IF EXISTS test_table;
CREATE TABLE test_table(
  id int,
  delete_column_1 string,
  delete_column_2 string)
PARTITIONED BY (
  dt string)
;

SET hive.exec.dynamic.partition.mode=nonstrict;
INSERT OVERWRITE TABLE test_table partition(dt) SELECT 1,'3','5','2019-01-01';
INSERT OVERWRITE TABLE test_table partition(dt) SELECT 2,'4','6','2019-01-02';

--------------------------------------------------
hive> SELECT * FROM test_table;
OK
1 3 5 2019-01-01
2 4 6 2019-01-02

用新的表结构来替换原来的表结构, HDFS 上的数据没有变化,但是 hive 就查不到多余的两列了。

1
2
3
4
5
6
7
8
9
10
11
ALTER TABLE `test_table` REPLACE COLUMNS(
  `order_id` string COMMENT 'ID')
;

--------------------------------------------------
hive> SELECT * FROM test_table;
OK
1 2019-01-01
2 2019-01-02

DROP TABLE IF EXISTS test_table;
本文由作者按照 CC BY 4.0 进行授权