当前位置: 首页 > article >正文

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

项目场景:

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

--查看
> 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过滤没有成功。

>  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类型字段进行过滤无效。

--查看表结构
> 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’ 进行过滤。

>  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         |
+------------+-----------------+

http://www.kler.cn/news/133674.html

相关文章:

  • C++ 虚函数和多态性
  • React整理总结(三)
  • 公司内部网络架设悟空CRM客户管理系统 cpolar无需公网IP实现内网,映射端口外网访问
  • 【测开求职】面试题:HR面相关的开放性问题
  • 基于Prometheus快速搭建网络质量监控平台
  • 2023_“数维杯”问题B:棉秸秆热解的催化反应-详细解析含代码
  • 计算机毕业设计选题推荐-点餐微信小程序/安卓APP-项目实战
  • Java Web——JavaScript基础
  • 高防IP是什么?如何隐藏源站IP?如何进行防护?
  • 代码随想录二刷 | 数组 | 总结篇
  • 03 前后端数据交互【小白入门SpringBoot + Vue3】
  • wpf devexpress在未束缚模式中生成Tree
  • IDEA写mybatis程序,java.io.IOException:Could not find resource mybatis-config.xml
  • 单元测试实战(六)其它
  • 【HarmonyOS开发】设备调试避坑指南
  • 三十一、W5100S/W5500+RP2040树莓派Pico<TCP_Server多路socket>
  • 别再吐槽大学教材了,来看看这些网友强推的数学神作!
  • 【我和Python算法的初相遇】——体验递归的可视化篇
  • SQL note2:DIsks and Files
  • linux如何一键自动安装系统(PXE)
  • 03_面向对象高级_多态
  • C++11的互斥包装器
  • 怎样助力IT运维团队更专业、更协作、更高效
  • 详解如何使用Jenkins一键打包部署SpringBoot项目
  • 华媒舍:怎样成为谷歌竞价排名羸家?10个方法
  • HIT 模式识别 手写汉字分类 Python实现
  • 038、语义分割
  • C++--哈希表--散列--冲突--哈希闭散列模拟实现
  • LintCode 1394 · Goat Latin (字符串处理题)
  • NET8 ORM 使用AOT SqlSugar