解决hive表新增的字段查询为空null问题

项目场景:

由于业务拓展,需要往hive分区表新增新的字段,hive版本为2.1.1-cdh6.3.2

于是利用

sql 复制代码
alter table table_name add columns (col_name string )

新增字段,然后向已存在分区中插入数据,以为问题就解决了。

结果一查询发现新增字段的值全部为null

这是怎么回事,怀疑是不是数据没有插入成功,于是查看日志确实是写入成功了,后换了impala和presto 两种引擎查询,发现两个结果都有值,如果直接到目录下查看数据文件会发现确实有值。

经排查,这是hive 的bug,用Hive版本比较低,会出现这个问题。据说最新的版本已经没有这个问题了(未验证)。


问题描述

为了复现这个问题,今天把这个问题追溯下。

1.新增一张学生测试表并向分区插入数据

sql 复制代码
create table if not exists test.student(id string comment '编号',user_name string comment '姓名',age int comment '年龄')comment '学生表'partitioned by(dt string comment '分区字段,格式yyyymmdd')stored as parquetTBLPROPERTIES('parquet.compression'='SNAPPY');
  • 其中dt为分区,往学生表新增一个分区,并插入记录测试。
sql 复制代码
insert overwrite table test.student partition (dt='20220112') select user_id, '小爱',7 from test.table_name limit 10

2.新增两个字段 class 、grade 并插入数据

sql 复制代码
alter table test.student add columns(class string);alter table test.student add columns(grade string);
sql 复制代码
insert overwrite table test.student partition (dt='20220112') select user_id, '小爱',7,'1班','一年级' from test.table_name limit 10

3.查询数据

sql 复制代码
select * from test.student where dt ='20220112'


发现刚新增的class grade 字段显示都为NULL,并不是我们期望。

但impala和presto 两种引擎查询是能够正常显示的。

4.再往表新增'20220113'分区

sql 复制代码
insert overwrite table test.student partition (dt='20220113') select user_id, '小爱',7,'1班','一年级' from test.table_name limit 10

5.再查询这个分区

sql 复制代码
select * from test.student where dt ='20220113'
  • 发现查询
sql 复制代码
select * from test.student where dt ='20220112'

还是依旧为NULL

  • 由此我们可以得出这样一个结论

分区在增加字段前存在,新增字段值为NULL的情况
分区在增加字段前不存在,正常


解决方案:

  • 1.删除分区或者重新建表

这种情况分区较多亦或是数据量较大,都不推荐使用。

  • 2.针对分区执行

对于在增加字段前已经存在的分区,需要再执行

sql 复制代码
alter table test.student partition(dt='20220112') add columns(grade string);alter table test.student partition(dt='20220112') add columns(class string);

我们再来看看'20220112'分区字段class和grade显示是否正常

sql 复制代码
select * from test.student where dt ='20220112'


从结果我们可以看到,已经正常显示了。

3.在往表添加字段时加上cascade

第二种方案,要是我们表里有很多分区,这样处理就显得有些繁琐了,不知有没有更优雅的处理方式,答案是肯定的,那就是在修改列时加上cascade

sql 复制代码
alter table test.student add columns (`number` string ) cascade;
sql 复制代码
insert overwrite table test.student partition (dt='20220113') select user_id, '小爱',7,'1班','一年级','N202209010101' from test.table_name limit 10
sql 复制代码
select * from test.student where dt ='20220113'

总结:

  • 1.对于在增加字段前已经存在的分区,需要再执行
sql 复制代码
alter table test.student partition(dt='20220112') add columns(column_name string);
  • 2.在往表添加字段时加上cascade
sql 复制代码
alter table test.student add columns (column_name string ) cascade;

个人觉得第二种解决方案操作比第一种要方便得多。推荐使用。


参考1

相关推荐
还是大剑师兰特4 小时前
Hadoop面试题及详细答案 110题 (96-105)-- Hadoop性能优化
hadoop·大剑师·hadoop面试题
ApacheSeaTunnel20 小时前
新兴数据湖仓手册·从分层架构到数据湖仓架构(2025):数据仓库分层的概念与设计
大数据·数据仓库·开源·数据湖·dataops·白鲸开源·底层技术
洛克大航海21 小时前
Ubuntu中使用Hadoop的HDFS和MapReduce
hadoop·ubuntu·hdfs·mapreduce
夫唯不争,故无尤也1 天前
Maven创建Java项目实战全流程
java·数据仓库·hive·hadoop·maven
想ai抽1 天前
深入starrocks-怎样实现多列联合统计信息
java·数据库·数据仓库
还是大剑师兰特1 天前
Hadoop面试题及详细答案 110题 (71-85)-- 集群部署与运维
大数据·hadoop·大剑师·hadoop面试题
派可数据BI可视化1 天前
商业智能BI与业务结构分析
大数据·数据仓库·信息可视化·数据分析·商业智能bi
_清浅1 天前
大数据平台基础(Hadoop大数据原理与应用)
大数据·hadoop·分布式
The Sheep 20232 天前
WPF自定义路由事件
大数据·hadoop·wpf
还是大剑师兰特2 天前
Hadoop面试题及详细答案 110题 (86-95)-- Hadoop生态系统工具
hadoop·大剑师·hadoop面试题