【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         |
+------------+-----------------+
相关推荐
hnlucky1 小时前
Windows 上安装下载并配置 Apache Maven
java·hadoop·windows·学习·maven·apache
尘世壹俗人8 小时前
hadoop.proxyuser.代理用户.授信域 用来干什么的
大数据·hadoop·分布式
2401_cf16 小时前
为什么hadoop不用Java的序列化?
java·hadoop·eclipse
钊兵1 天前
hivesql是什么数据库?
大数据·hive
RestCloud1 天前
产品更新丨谷云科技 iPaaS 集成平台 V7.5 版本发布
数据仓库·系统安全·api·数字化转型·ipaas·数据集成平台·集成平台
RestCloud1 天前
数据清洗(ETL/ELT)原理与工具选择指南:企业数字化转型的核心引擎
数据仓库·数据安全·etl·数据集成·elt·集成平台
wingaso1 天前
[经验总结]删除gitlab仓库分支报错:错误:无法推送一些引用到“http:”
linux·数据仓库·git
线条11 天前
MapReduce Shuffle 全解析:从 Map 端到 Reduce 端的核心数据流
大数据·hadoop·mapreduce
火龙谷2 天前
【hadoop】Kafka 安装部署
大数据·hadoop·kafka
火龙谷2 天前
【hadoop】Flume的相关介绍
大数据·hadoop·flume