在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