zoukankan      html  css  js  c++  java
  • hive 空值判断

    hive中空值判断基本分两种

    一、NULL 与 N

    hive在底层数据中如何保存和标识NULL,是由 alter table name SET SERDEPROPERTIES('serialization.null.format' = 'N'); 参数控制的
    比如:
    1、设置 alter table name SET SERDEPROPERTIES('serialization.null.format' = 'N'); 
    则:底层数据保存的是'N',通过查询显示的是'NULL'
    这时如果查询为空值的字段可通过 语句:a is null 或者 a='N'
    2、测试

    hive> create table test(id int,name string);
    
    hive> insert into table test(1, 'yjt');
    
    hive> alter  table test set serdeproperties("serialization.null.format"='N');
    
    hive> insert into table test values(2, 'yjl');
    
    hive> insert into table test values(3, NULL);
    
    hive> select * from test;
    OK
    test.id	test.name
    1	yjt
    2	yjl
    3	NULL
    
    hive> select * from test where name is NULL;
    OK
    test.id	test.name
    3	NULL
    
    hive> select * from test where name = '\N';    #修改存储空值时为'N',但是在查询的时候,不能使用'\N'的方式判断
    OK
    test.id	test.name
    Time taken: 0.182 seconds
    

    2.设置 alter tablename SET SERDEPROPERTIES('serialization.null.format' = 'NULL'); 
    则:底层数据保存的是'NULL',通过查询显示的是'NULL'
    这时如果查询为空值的字段可通过 语句:a is null 或者 a='NULL'

    hive> alter  table test set serdeproperties("serialization.null.format"='NULL');
    hive> select * from test ;
    OK
    test.id	test.name
    1	yjt
    2	yjl
    3	N      #以前存储的NULL值变成了 'N'
    
    
    查询
    hive> select * from test where name = 'N';
    OK
    test.id	test.name
    3	N
    
    重新插入空值测试
    hive> insert into table test values(6, NULL);
    
    hive> select * from test;
    OK
    test.id	test.name
    1	yjt
    2	yjl
    3	N
    4	N
    5	
    6	NULL
    Time taken: 1.182 seconds, Fetched: 6 row(s)
    hive> select * from test where name is null;    #通过is null 的方式可以查到
    OK
    test.id	test.name
    6	NULL
    Time taken: 0.146 seconds, Fetched: 1 row(s)
    hive> select * from test where name = 'null';
    OK
    test.id	test.name
    Time taken: 0.151 seconds
    hive> select * from test where name = 'NULL';  #这种方式不能查询到存储的空值
    OK
    test.id	test.name
    Time taken: 0.171 seconds
    hive> 
    
    

    总结 最好还是使用is null判断

    二、'' 与 length(xx)=0

    '' 表示的是字段不为null且为空字符串,此时用 a is null 是无法查询这种值的,必须通过 a=''  或者 length(a)=0 查询

    hive> 
        > 
        > select * from test where length(name) = 0;
    OK
    test.id	test.name
    5	
    Time taken: 0.199 seconds, Fetched: 1 row(s)
    hive> select * from test where name = '';
    OK
    test.id	test.name
    5	
    Time taken: 0.182 seconds, Fetched: 1 row(s)
    
    

    总结,如果是空字符串,可以使用=或者length方式判断

    原文链接:https://blog.csdn.net/jyl1798/article/details/41871013

  • 相关阅读:
    SICP习题 1.11 (一个函数的递归与迭代)
    SICP 实例 ExchangeMoney
    SICP 1.2.2 树形递归 与 线性迭代(斐波那契数)
    SICP习题 1.10(Ackermann函数)
    SICP习题 1.9 (递归与迭代初探)
    SICP实例 1.2.1 (阶乘的递归与迭代)
    SICP习题 1.8 (立方根)
    SICP习题 1.7 (求平方根改进)
    SICP习题 1.6 (再探 函数 与 正则序 应用序 关系)
    SICP实例 1.1.7 (求平方根)
  • 原文地址:https://www.cnblogs.com/yjt1993/p/13207080.html
Copyright © 2011-2022 走看看