解决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

相关推荐
梦里不知身是客116 小时前
spark读取table中的数据【hive】
大数据·hive·spark
DashVector7 小时前
向量检索服务 DashVector产品计费
数据库·数据仓库·人工智能·算法·向量检索
yumgpkpm14 小时前
Doris在CMP7(类Cloudera CDP 7 404版华为Kunpeng)启用 Kerberos部署Doris
大数据·hive·hadoop·python·oracle·flink·cloudera
Mr_Art8915 小时前
金融行业湖仓实践:Apache Paimon 小文件治理之道
数据仓库·金融·apache
数据牧羊人的成长笔记20 小时前
Hadoop 分布式计算MapReduce和资源管理Yarn
hadoop·eclipse·mapreduce
帅次20 小时前
系统分析师-案例分析-数据库系统&数据仓库&反规范化技术&NoSQL&内存数据库
大数据·数据库·数据仓库·oracle·kafka·数据库开发·数据库架构
新疆嘉博智选科技有限公司20 小时前
Macos系统上搭建Hadoop详细过程
大数据·hadoop·分布式
计算机编程-吉哥1 天前
大数据毕业设计项目推荐 基于大数据的广西药店数据可视化分析系统 1.65w条数据【大数据毕业设计项目选题】
大数据·hadoop·毕业设计·计算机毕设·大数据毕业设计选题推荐
小湘西1 天前
在 Hive 中NULL的理解
数据仓库·hive·hadoop
牛奶咖啡131 天前
zabbix实现监控Hadoop、Docker、SSL证书过期时间应用的保姆级实操流程
hadoop·zabbix·docker-ce引擎安装·监控docker容器·监控ssl证书的过期时间·监控hadoop·安装配置agent2