表达式 "IS NOT NULL" 不适用于 HQL

Expression "IS NOT NULL" not working on HQL

当我对配置单元 table 上的非空值执行 select 语句时,响应中没有正确的结果。结果好像 "is not null" 表达式不存在!

例子:

select count(*)
from test_table
where test_col_id1='12345' and test_col_id2 is not null;

注意 test_col_id1test_col_id2 不是分区键。

这是我的配置单元版本。

Hive 0.14.0.2.2.0.0-2041

这是table:

... | test_col_id1 | test_col_id2 |
... | 12345 | × |
... | 12345 |空 |

本次查询returns2条记录。

试试下面的查询,它有 return 行吗?

select count(*)
from test_table
where test_col_id1='12345' and test_col_id2 != 'NULL';

那么你的NULL不是NULL,而是字符串'NULL'。很多人对 NULL 字符串的 Hive 处理有问题。默认情况下,它是空字符串 ''。如果我们想要其他任何东西,我们必须在创建 table 时准确指定 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;

由于您已经创建了 table,因此您可以更改 table 以便它将您的 'NULL' 识别为 NULL:

ALTER TABLE test_table SET TBLPROPERTIES ('serialization.null.format' = 'NULL');

Hive 以不同的方式解析 NULL 值。

Hive 不考虑空值为 NULL。但是,默认情况下,Hive 使用 \N 来表示 NULL(不是特殊字符,只是反斜杠加上大写字母 N)。 如果只想过滤掉所有空值,可以使用

where test_col_id2 != ''

如果您在加载到 hive table 的原始文本文件中使用了真正的 NULL 值,您可以尝试

where test_col_id2 != '[=13=]0'

因为NULL在八进制中的ASCII码是00,第一个“0”表示是八进制数

顺便说一句,你也可以使用命令:

alter table $YOUR_TABLE SETSERDEPROPERTIES('serialization.null.format' ='abc')

如果您希望 Hive 将 "abc" 解析为某些 table.

的 NULL 值,则将 NULL 值自定义为 "abc"