【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         |
+------------+-----------------+
相关推荐
jiuweiC5 小时前
hive常用命令
hive
hweiyu009 小时前
Hive 技术深度解析与 P7 数据分析架构师多行业全场景实战课程合集(视频教程)
hive·数据分析
qq_12498707531 天前
基于hadoop的电商用户行为分析系统(源码+论文+部署+安装)
大数据·hadoop·分布式·毕业设计
临风赏月1 天前
Hadoop、Kafka、Flink、Spark、Hive五大组件运维常用操作命令
hadoop·flink·kafka
呆呆小金人1 天前
SQL视图:虚拟表的完整指南
大数据·数据库·数据仓库·sql·数据库开发·etl·etl工程师
杰克逊的日记1 天前
StarRocks数据仓库
starrocks·数据仓库·mpp
工具人55551 天前
adb disable-verity
数据库·数据仓库·adb
dundunmm1 天前
【每天一个知识点】数据湖(Data Lake)与数据仓库(Data Warehouse)
数据仓库·数据湖
笨蛋少年派1 天前
Hive安装部署
数据仓库·hive·hadoop
呆呆小金人2 天前
SQL键类型详解:超键到外键全解析
大数据·数据库·数据仓库·sql·数据库开发·etl·etl工程师