zoukankan      html  css  js  c++  java
  • hive中的null

    在处理流水增量表的时候,出现了一个判定的失误。

    select a.a1,a.a2
      from
        (
        select 
          a.a1
          ,if(a.a2<>b.b2,1,0) as diff
         ,a.a2
        from a 
        lefter join b 
        on a.a1=b.b1
        ) c
    where c.diff=1;

    因为左外关联,可能会出现b表数据不存在 则b.b2 is null , if(a.a2<>b.b2,1,0) as diff,null值的判断只能使用is ,is not

    0: jdbc:hive2://localhost:10000/big12> select if(1 <>null,0,1);
    +------+--+
    | _c0  |
    +------+--+
    | 1    |
    +------+--+
    1 row selected (0.13 seconds)
    0: jdbc:hive2://localhost:10000/big12> select 1<>null;
    +-------+--+
    |  _c0  |
    +-------+--+
    | NULL  |
    +-------+--+
    1 row selected (0.127 seconds)

    所以处理方式:

    0: jdbc:hive2://localhost:10000/big12> select if(1<>null or null is null,1,0) as diff;
    +-------+--+
    | diff  |
    +-------+--+
    | 1     |
    +-------+--+
    1 row selected (0.121 seconds)
    0: jdbc:hive2://localhost:10000/big12> select if(1<>nvl(null,0),1,0) as diff;
    +-------+--+
    | diff  |
    +-------+--+
    | 1     |
    +-------+--+
    1 row selected (0.13 seconds)

    其他:

    employee表

    hive>desc employee;
    empid   string
    deptid   string
    salary   string

    查询employee

    hive>select * from employee
    1 NULL NULL

    hive 中null实际在HDFS中默认存储为'N'(但是我们一般为了安全性把null的储存格式调整为'')
    即employee中的数据在HDFS中为
    1 N N
    验证,插入'N'

    hive>insert into table employee select '2','\N','\N' from employee limit 1;

    其中多一个斜杠是转义的作用
    查询employee

    hive>select * from employee
    1 NULL NULL
    2 NULL NULL

    此时hive中与null有关的函数,如nvl,coalesce,is null等判断是否为null是为true

    hive>select nvl(empid,'A'),nvl(deptid,'B'),nvl(salary,'C') from employee
    1 B C
    2 B C

    但是null或NULL和''会被hive当做字符串处理。

    hive>insert into table employee select '3','','' from employee limit 1;

    查询:

    hive>select * from employee;
    1 NULL NULL
    2 NULL NULL
    3    
    hive>insert into table employee select '4','null','NULL' from employee limit 1;

    查询

    hive>select * from employee;
    1 NULL NULL
    2 NULL NULL
    3    
    4 null NULL


    注意:1,2同一行的NULL与4行的NULL或null不一样。4行的NULL或null为字符串
    此时hive中与null有关的函数,如nvl,coalesce,is null等判断''和null(字符串)或NULL(字符串)是否为null是为false

    hive> select empid ,nvl(deptid,'E'),nvl(salary,'F') from employee;
    1 E F
    2 E F
    3    
    4 null NULL
    hive>select * from employee where deptid='';
    3    
    hive>select * from employee where deptid='null' and salary ='NULL';
    4 null NULL
    hive>select * from employee where deptid is null;
    1 NULL NULL
    2 NULL NULL


    可以通过

    ALTER TABLE table_name SET SERDEPROPERTIES('serialization.null.format' = 定义描述符);

    修改空值描述符
    如果将''定义为NULL

    ALTER TABLE employee SET SERDEPROPERTIES('serialization.null.format' = '');


    查询employee

    hive>select * from employee;
    1 N N
    2 N N
    3 NULL NULL
    4 null NULL


    和前面的select比较发现''变成了NULL,而N露出了真面目,4行的NULL或null为字符串没变化
    验证,将''插入到emloyee

    hive> insert into table employee select '5','','' from employee limit 1;


    查询

    hive>select * from employee;
    1 N N
    2 N N
    3 NULL NULL
    4 null NULL
    5 NULL NULL


    注意:3,5同一行的NULL与4行的NULL或null不一样。4行的NULL或null为字符串
    此时HDFS中如此存储

    1 N N
    2 N N
    3  
    4 null NULL
    5  


    此时

    hive> select empid ,nvl(deptid,'E'),nvl(salary,'F') from employee;
    1 N N
    2 N N
    3 E F
    4 null NULL
    5 E F


    总结hive中null的定义的意义在于:oracle数据导出后原表中的null会变成'',然后导入到hive中也变成了''。但是hive中关于NULL的一些函数如nvl,coalesce和is null却无法使用,因为hive默认N才代表NULL。在hive中通过
    ALTER TABLE SET SERDEPROPERTIES('serialization.null.format' = '');修改''代表NULL,改造存储过程中就不需要改nvl等语句。

  • 相关阅读:
    EntityFramework4.5使用Expression类创建动态查询及动态查询导航属性
    EF 5.0 帮助类
    EF异常:“System.InvalidOperationException”类型的未经处理的异常在 mscorlib.dll 中发生
    EF框架学习手记
    Entity Framework 学习
    C#特性-表达式树
    LINQ to SQL 运行时动态构建查询条件
    一点css 基础
    JQuery 判断复选框是否选中
    Asp.Net Server.MapPath()用法
  • 原文地址:https://www.cnblogs.com/wqbin/p/11168600.html
Copyright © 2011-2022 走看看