hive 更改列位置后无法查询表

rbl8hiat  于 2022-11-05  发布在  Hive
关注(0)|答案(1)|浏览(210)

当使用“select * from t2p”查询表时,响应如下所示。我想我遗漏了一些概念,请帮助我。
发生异常java.io. IO异常:java.lang.无法将对象检查器强制转换为原始对象检查器
步骤1,创建表

create table t2p(id int, name string, score map<string,double>) 
partitioned by (class int)
row format delimited
fields terminated by ','
collection items terminated by '\\;'
map keys terminated by ':'
lines terminated by '\n'
stored as textfile;

步骤2,插入数据,如

1,zs,math:90.0;english:92.0
2,ls,chinese:89.0;math:80.0
3,xm,geo:87.0;math:80.0
4,lh,chinese:89.0;english:81.0
5,xw,physics:91v;english:81.0

步骤3,添加另一列

alter table t2p add columns (school string);

步骤4,更改列顺序

alter table t2p change school school string after name;

第五步,执行查询并得到上述错误。

select * from t2p;
oyt4ldly

oyt4ldly1#

这是一个明显的错误。您的命令alter table t2p change school school string after name;只会变更中继数据。如果您要移动数据行,数据必须已经符合新的结构描述,或者您必须以其他方式加以变更以符合。
这意味着,Map列必须与新列匹配。换句话说,如果要移动列,请确保新列和现有列的数据类型相同。
我用int数据类型做了一个简单的实验。它成功了,因为数据类型没有太大的不同,但是你可以看到元数据改变了,但是数据保持不变。

create table t2p(id int, name string, score int) 
partitioned by (class int)
stored as textfile;
insert into t2p partition(class=1) select 100,'dum', 199;
alter table t2p add columns (school string);
alter table t2p change school school string after name;
MSCK REPAIR TABLE t2p ;
select * from t2p;

你可以看到新的列校被Map到位置3(定义为INT)。

解决方案-您可以这样做,但要确保新结构+数据类型与旧结构兼容。

相关问题