当我对 hive 表上的非空值执行 select 语句时,响应中没有正确的结果。结果就好像“不为空”表达式不存在一样!
示例 :
select count(*)
from test_table
where test_col_id1='12345' and test_col_id2 is not null;
备注
test_col_id1
和 test_col_id2
不是分区键。这是我的 hive 版本。
Hive 0.14.0.2.2.0.0-2041
这是表:
... | test_col_id1 | test_col_id2 |
... | 12345 | × |
... | 12345 |空 |
此查询返回 2 条记录。
最佳答案
试试下面的查询,它是否返回行?
select count(*)
from test_table
where test_col_id1='12345' and test_col_id2 != 'NULL';
那么你的
NULL
不是 NULL
,它是字符串 'NULL'。很多人对 NULL
的 Hive 治疗有问题字符串。默认情况下,它是空字符串 ''
.如果我们想要其他任何东西,我们必须在创建表时准确指定处理 NULL 字符串的方式。以下是如何更改识别为 NULL
的 3 个示例。 .第一个将 'NULL' 字符串设置为 NULL
:CREATE TABLE nulltest1 (id STRING, another_string STRING)
TBLPROPERTIES('serialization.null.format'='NULL') --sets the string 'NULL' as NULL;
CREATE TABLE nulltest2 (id STRING, another_string STRING)
TBLPROPERTIES('serialization.null.format'='') --sets empty string as NULL;
CREATE TABLE nulltest3 (id STRING, another_string STRING)
TBLPROPERTIES('serialization.null.format'='\N'); --sets \N as NULL;
由于您已经创建了您的表格,您可以更改您的表格,以便它能够识别您的
'NULL'
如 NULL
:ALTER TABLE test_table SET TBLPROPERTIES ('serialization.null.format' = 'NULL');
关于null - 表达式 "IS NOT NULL"不适用于 HQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37323379/