• 【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         |
    +------------+-----------------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    问题描述

    使用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  |
    +------------+-----------------+
    +------------+-----------------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行的结果都是为空的,并没有将classes为null或者NULL对应的id过滤出来。


    原因分析:

    使用 is null / is not null 对string类型字段进行过滤无效。

    --查看表结构
    > desc test_1;
    
    +-----------+------------+----------+
    | col_name  | data_type  | comment  |
    +-----------+------------+----------+
    | id        | string     |          |
    | classes   | string     |          |
    +-----------+------------+----------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    可以看到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         |
    +------------+-----------------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    项目管理软件dhtmlxGantt配置教程(六):编辑器的类型
    高通导航器软件开发包使用指南(10)
    在el-table表头上引入组件不能实时传参bug
    后台获取不到请求头中token信息的解决方法
    算法竞赛备赛之贪心算法训练提升,贪心算法基础掌握
    python网络爬虫笔记15:使用js验证获取网页的请求头信息和请求参数
    Java基础面试题50题
    病毒感染检测(运用BF算法)
    金仓数据库KingbaseES客户端编程接口指南-ado.net(11. 等待通知)
    什么是人工智能,它是如何使用的?
  • 原文地址:https://blog.csdn.net/sodaloveer/article/details/134412462