在HIVE中进行NULL检查

时间:2018-03-16 16:17:42

标签: hadoop hive

我在hive查询中遇到了一个奇怪的行为。在根据输入参数插入值时,我将属性设置为NULL。当我执行count

select count(id) from hive_table where val1 is NULL;

结果是0。但我知道大约有7.5 K记录被设置为NULL。而以下查询返回准确的结果

select count(id) from hive_table where trim(val1) is NULL;

我正在使用Apache Hive 1.2版和Hortonworks Hadoop 2.7。任何想法为什么会这样。

3 个答案:

答案 0 :(得分:0)

count会忽略null个值,这就是您输出0的原因。你应该计算行数。

select count(*) from hive_table where val1 is NULL;

编辑:我想你将空字符串解释为null s。试试这个来检查。

select sum(cast(trim(val1)='' as int)) as empty_str_count
,sum(cast(val1 is null as int)) as null_count
from hive_table 

答案 1 :(得分:0)

我不知道为什么第二个有效。但是,如果您想计算NULL个值,请使用:

select count(*)
from hive_table
where val1 is NULL;

答案 2 :(得分:0)

你可以尝试另一种方法从蜂巢中找到空记录数

select count(id) from hive_table where length(val1)=0;