Hive Table 操作命令汇总 列出了所有的语法,但是在不同的文件格式下没有测试,是否支持事务也没有测试。
Text 格式测试
Text 格式所有的数据存储在 text 格式,数据不存储列名,使用时转换为相应的对应关系,文本中的第 x 列解析为表的第 x 列。
- 改位置
drop table if exists t1_t;
create table t1_t(c1 int,c2 int) stored as textfile;
insert into t1_t values(1,2);
alter table t1_t CHANGE COLUMN c1 c1 int after c2;
select * from t1_t;
可以看到,结果仍然可以显示。现在第1列是 c2, 第 2 列变成了 c1。
hive> select * from t1_t;
OK
1 2
- 改类型
drop table if exists t1_t;
create table t1_t(c1 int,c2 int) stored as textfile;
insert into t1_t values(1,2);
alter table t1_t CHANGE COLUMN c1 c1 varchar(255);
select * from t1_t;
可以看到,结果仍然可以显示。
hive> select * from t1_t;
OK
1 2
- 仅改字段名
drop table if exists t1_t;
create table t1_t(c1 int,c2 int) stored as textfile;
insert into t1_t values(1,2);
alter table t1_t CHANGE COLUMN c1 c1_1 int;
select * from t1_t;
可以看到,结果仍然可以显示。文本中的第 x 列解析为表的第 x 列。
hive> select * from t1_t;
OK
1 2
ORC 文件格式(非事务)
ORC 格式所有的数据用固定的格式存储,是强类型的。
- 改位置
drop table if exists t1_o;
create table t1_o(c1 int,c2 int) stored as orc;
insert into t1_o values(1,2);
alter table t1_o CHANGE COLUMN c1 c1 int after c2;
select * from t1_o;
输出
hive> select * from t1_o;
OK
2 1
可以看到,和 textfile 结果不一样。因为数据文件中存储了列名。
- 改类型
drop table if exists t1_o;
create table t1_o(c1 int,c2 int) stored as orc;
insert into t1_o values(1,2);
alter table t1_o CHANGE COLUMN c1 c1 VARCHAR(255);
可以看到,修改类型之后,列 c1 数据还是可以读取,读取之后转换为现在的类型,可以用 varchar 的函数,如 length,c2 列不能用 length 函数。
hive> select * from t1_o;
OK
1 2
- 仅改字段名
drop table if exists t1_o;
create table t1_o(c1 int,c2 int) stored as orc;
insert into t1_o values(1,2);
alter table t1_o CHANGE COLUMN c1 c1_1 int;
可以看到,修改字段名之后,列 c1_1 列的数据为 NULL。
hive> select * from t1_o;
OK
NULL 2
ORC 文件格式(事务表)
- 改位置
drop table if exists t1_o2;
create table t1_o2(c1 int,c2 int) stored as orc tblproperties('transactional'='true');
insert into t1_o2 values(1,2);
alter table t1_o2 CHANGE COLUMN c1 c1 int after c2;
可以看到,因为数据存储的复杂(base 文件,delete 文件,delta 文件)事务表不运行改列位置。
hive> alter table t1_o2 CHANGE COLUMN c1 c1 int after c2;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Reordering columns is not supported for table test.t1_o2. SerDe may be incompatible
- 改类型
drop table if exists t1_o2;
create table t1_o2(c1 int,c2 int) stored as orc tblproperties('transactional'='true');
insert into t1_o2 values(1,2);
alter table t1_o2 CHANGE COLUMN c1 c1 varchar(250);
hive> select * from t1_o2;
OK
1 2
可以看到,改类型可以。
- 仅改字段名
drop table if exists t1_o2;
create table t1_o2(c1 int,c2 int) stored as orc tblproperties('transactional'='true');
insert into t1_o2 values(1,2);
alter table t1_o2 CHANGE COLUMN c1 c1_1 int;
成功,但是列 c1_1 内容为 NULL,因为 ORC 格式的数据把列名写入了文件。
hive> select * from t1_o2;
OK
NULL 2