【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         |
+------------+-----------------+
相关推荐
一路向北finish7 分钟前
《Hadoop 高可用集群与 OpenStack Mitaka 控制节点部署全流程实操排坑指南》
大数据·linux·运维·服务器·hadoop·openstack
insertYam28 分钟前
[hadoop高可用架构]
大数据·hadoop·架构
hopsky1 小时前
《Hive性能调优实战》核心内容详细解读
数据仓库·hive·hadoop
楠楠子呀2 天前
独立站聊天转化全链路:从二维码引流到数据复盘
java·javascript·数据仓库·python·c#·自动化·etl
-今昭-2 天前
Hadoop HDFS 高可用实战指南
运维·hadoop·hdfs·负载均衡
熊出没3 天前
AI经营分析数据中台如何搭建数仓
数据仓库
一路向北finish3 天前
《CentOS 7 搭建 Hadoop 3.3.6 分布式集群:NFS 共享方案完整实操》
linux·运维·hadoop·分布式·centos
BYSJMG3 天前
大数据毕业设计选题方向|【基于大数据的燃油消耗数据可视化与分析】Hadoop+PySpark+FP-Growth
大数据·hadoop·分布式·信息可视化·数据分析·课程设计
熊出没3 天前
解密数仓中的ODS、DWD、DWS、ADS
数据库·数据仓库
等一个人的@4 天前
数睿通2.0更新:可视化ETL引擎重构与部署优化
数据仓库·重构·etl