Hive 空值和NULL字符串 踩坑

在hive测试环境中发现,通过csv上传到hive中的空值字段会被转化为NULL字符串,在查询的时候where xxx is null 查不到数据 通过 where xxx = ‘NULL’ 能查到数据

  • 复现问题:
CREATE TABLE `call_v2`( `id` string,
                                        `call_uuid` string,
                                        `transaction_id` string,
                                        `type` string,
                                        `status` string,
                                        `caller_number` string,
                                        `dest_number` string,
                                        `call_create_time` string,
                                        `call_answer_time` string,
                                        `call_hangup_time` string,
                                        `ring_time` string,
                                        `duration` string,
                                        `bill_sec` string,
                                        `talk_time` string,
                                        `hangup_cause` string,
                                        `hangup_cause_detail` string,
                                        `record_key` string,
                                        `read_record_key` string,
                                        `write_record_key` string,
                                        `create_time` string,
                                        `update_time` string) PARTITIONED BY (`dt` string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE TBLPROPERTIES("skip.header.line.count"="1");
  • 上传数据
load data local inpath '/home/yqg/liuzhiwei/query-hive-199233.csv' into table call_v2 Partition (dt='20200601');
  • 问题显示
    在这里插入图片描述
    在这里插入图片描述

  • 解决方法:

alter table call_v2 SET SERDEPROPERTIES('serialization.null.format' = 'NULL'); 
  • 导致的原因:

hive中空值判断基本分两种

1. NULL 与 \N

hive在底层数据中如何保存和标识NULL,是由 alter table name SET SERDEPROPERTIES(‘serialization.null.format’ = ‘\N’); 参数控制的
比如:

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

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

2.’’ 与 length(xx)=0

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

  • 参考: https://www.cnblogs.com/qiuhong10/p/8119546.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章