Hive 在不同文件格式和是否事务表下修改列操作

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值