【hive遇到的坑】—使用 is null / is not null 对string类型字段进行null值过滤无效

项目场景:

查看测试表test_1,发现表字段classes里面有null值,过滤null值。

sql 复制代码
--查看
> select * from test_1;
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
| Mary       | class 1         |
| James      | class 2         |
| lily       | null            |
| Mike       | NULL            |
| Herry      | class 1         |
+------------+-----------------+

问题描述

使用where classes is null过滤没有成功。

sql 复制代码
>  select * from test_1 where classes is null;
>  select * from test_1 where classes is NULL;
>  select * from test_1 where classes is not null;
>  select * from test_1 where classes is not NULL;

--运行结果:
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
+------------+-----------------+

运行的结果都是为空的,并没有将classes为null或者NULL对应的id过滤出来。


原因分析:

使用 is null / is not null 对string类型字段进行过滤无效。

sql 复制代码
--查看表结构
> desc test_1;

+-----------+------------+----------+
| col_name  | data_type  | comment  |
+-----------+------------+----------+
| id        | string     |          |
| classes   | string     |          |
+-----------+------------+----------+

可以看到classes的类型是string,hive的底层保存的是'null'、'NULL'是个字符串,想要过滤掉null或者NULL值,使用is not null无效。


解决方案:

对于字符串字段,使用 ='null',='NULL',!= 'null',!= 'NULL' 进行过滤。

sql 复制代码
>  select * from test_1 where classes = 'null';
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
| lily       | null            |
+------------+-----------------+

>  select * from test_1 where classes = 'NULL';
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
| Mike       | NULL            |
+------------+-----------------+

>  select * from test_1 where classes != 'null';
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
| Mary       | class 1         |
| James      | class 2         |
| Mike       | NULL            |
| Herry      | class 1         |
+------------+-----------------+

>  select * from test_1 where classes != 'NULL';
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
| Mary       | class 1         |
| James      | class 2         |
| lily       | null            |
| Herry      | class 1         |
+------------+-----------------+
相关推荐
好大哥呀3 小时前
Hadoop yarn
大数据·hadoop·分布式
红队it4 小时前
【数据分析】基于Spark链家网租房数据分析可视化大屏(完整系统源码+数据库+开发笔记+详细部署教程+虚拟机分布式启动教程)✅
java·数据库·hadoop·分布式·python·数据分析·spark
本旺1 天前
【数据开发离谱场景记录】Hive + ES 复杂查询场景处理
hive·hadoop·elasticsearch
莫叫石榴姐1 天前
Doris为2.1版本,但json_each不可以用解决方法
数据仓库·json
无泪无花月隐星沉1 天前
uos server 1070e部署Hadoop
大数据·运维·服务器·hadoop·分布式·uos·国产化os
悟能不能悟2 天前
springboot全局异常
大数据·hive·spring boot
是阿威啊2 天前
【第一站】本地虚拟机部署Hadoop分布式集群
大数据·linux·hadoop·分布式
lightningyang2 天前
Hadoop 分布式集群配置(OpenEuler 1主2)
hadoop·openeuler·天枢一体化虚拟仿真靶场平台
是阿威啊2 天前
【第六站】测试本地项目连接虚拟机上的大数据集群
大数据·linux·hive·hadoop·spark·yarn
老徐电商数据笔记2 天前
技术复盘第八篇:从“数据烟囱”到“能力引擎”:中型电商数仓重构实战手册
大数据·数据仓库·重构·数据中台·用户画像·技术面试