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

Hive分区表新增字段,查询时数据为NULL的解决方案

由于业务拓展,需要往hive分区表新增新的字段,hive版本为2点多。

于是利用

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

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

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

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

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

1. 问题追溯

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

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

复制代码
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为分区,往学生表新增一个分区,并插入记录测试。

复制代码
insert overwrite table test.student partition (dt='20220112') select user_id, '小爱',7 from test.table_name limit 10

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

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

3.查询数据

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

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

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

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

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

5.再查询这个分区

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

发现查询

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

还是依旧为NULL

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

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

分区在增加字段前不存在,正常

3.解决方案

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

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

2.针对分区执行

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

复制代码
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显示是否正常

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

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

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

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

复制代码
alter table test.student add columns (`number` string ) cascade;

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

select * from test.student where dt ='20220113'

总结:

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

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

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

复制代码
alter table test.student add columns (column_name string ) cascade;

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

由于笔者知识及水平有限,文中错漏之处在所难免,如有不足之处,欢迎交流。

相关推荐
SelectDB技术团队2 小时前
从 ClickHouse、Druid、Kylin 到 Doris:网易云音乐 PB 级实时分析平台降本增效
大数据·数据仓库·clickhouse·kylin·实时分析
itachi-uchiha4 小时前
Docker部署Hive大数据组件
大数据·hive·docker
viperrrrrrrrrr75 小时前
大数据学习(131)-Hive数据分析函数总结
大数据·hive·学习
Leo.yuan8 小时前
API是什么意思?如何实现开放API?
大数据·运维·数据仓库·人工智能·信息可视化
qq_408413399 小时前
spark 执行 hive sql数据丢失
hive·sql·spark
TDengine (老段)12 小时前
TDengine 替换 Hadoop,彻底解决数据丢失问题 !
大数据·数据库·hadoop·物联网·时序数据库·tdengine·涛思数据
workflower14 小时前
量子比特实现方式
数据仓库·服务发现·需求分析·量子计算·软件需求
yt948321 天前
如何在IDE中通过Spark操作Hive
ide·hive·spark
青春之我_XP1 天前
【基于阿里云搭建数据仓库(离线)】Data Studio创建资源与函数
大数据·数据仓库·sql·dataworks·maxcompute·data studio
Leo.yuan1 天前
实时数据仓库是什么?数据仓库设计怎么做?
大数据·数据库·数据仓库·数据分析·spark