【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         |
+------------+-----------------+
相关推荐
qq_13948428821 天前
基于Python网易云排行榜数据分析系统设计与实现
大数据·hadoop·python·django·flask
走遍西兰花.jpg1 天前
sqoop的导入导出
hive·hadoop·sqoop
kong79069281 天前
Hadoop介绍HDFS介绍
大数据·hadoop·分布式
是阿威啊1 天前
【用户行为归因分析项目】- 【企业级项目开发第四站】模拟三类用户行为数据上传到Hadoop
大数据·hadoop·分布式·sql·scala
LF3_1 天前
Centos7,搭建Hive3.1.3数据库
大数据·数据库·hive
走遍西兰花.jpg2 天前
如何启动azkaban
hadoop·azkaban
一只大侠的侠2 天前
数据工程新范式“从ETL到ELT的平滑迁移实战指南”
数据仓库·etl
是阿威啊2 天前
【用户行为归因分析项目】- 【企业级项目开发第二站】项目通用代码开发
大数据·服务器·数据仓库·hive·hadoop
大千AI助手2 天前
HiveOperator 中 hql 模板路径解析失败的原因分析
hive·python·任务调度·airflow·模版·大千ai助手·hiveoperator
Hello.Reader2 天前
Hive Dialect 的查询能力支持哪些 HiveQL 子集,怎么写、怎么跑
数据仓库·hive·hadoop