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

标签 null hive hql

当我对 hive 表上的非空值执行 select 语句时,响应中没有正确的结果。结果就好像“不为空”表达式不存在一样!

示例 :

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

备注 test_col_id1test_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/

相关文章:

swift - 在枚举中生成随机类型的静态函数会导致崩溃并出现错误 "unexpectedly found nil when unwrapping an Optional value"

java - 返回 null 的方法导致 java.lang.NullPointerException

JavaScript - 检查数组中的所有值是否都不是 Null - Array/Each/

java - Hive 查询在 INSERT OVERWRITE 上失败

c# - 什么是NullReferenceException,如何解决?

java - 无法从Spark SQL插入到Hive分区表

flutter - Flutter Hive:制作多个openBox

java - 在 MySQL 数据库上使用 HQL 使用子查询(联接到同一个表)进行更新时出现问题。

mysql - HQL 查询从两个表中获取详细信息

c# - 如何查询 NHibernate 中的多对多关联?