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

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

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

相关推荐
大数据CLUB21 小时前
基于spark的奥运会奖牌变化数据分析
大数据·hadoop·数据分析·spark
Edingbrugh.南空21 小时前
Hadoop高可用集群搭建
大数据·hadoop·分布式
无级程序员2 天前
hive2服务启动报错:/tmp/hive on HDFS should be writable(不是chmod 777能解决的)
hive·hadoop·hdfs
rui锐rui2 天前
大数据学习2:HIve
大数据·hive·学习
凌辰揽月2 天前
Servlet学习
hive·学习·servlet
weixin_307779132 天前
Hive集群之间迁移的Linux Shell脚本
大数据·linux·hive·bash·迁移学习
王小王-1233 天前
基于Hadoop的公共自行车数据分布式存储和计算平台的设计与实现
大数据·hive·hadoop·分布式·hadoop公共自行车·共享单车大数据分析·hadoop共享单车
王小王-1233 天前
基于Hadoop的大规模文本词频统计分析系统设计与实现
hadoop·mapreduce·hadoop词频统计·hadoop文本统计·mapreduce词频统计
陈敬雷-充电了么-CEO兼CTO3 天前
推荐算法系统系列>推荐数据仓库集市的ETL数据处理
大数据·数据库·数据仓库·数据挖掘·数据分析·etl·推荐算法
桂成林3 天前
Hive UDF 开发实战:MD5 哈希函数实现
hive·hadoop·哈希算法