【hive遇到的坑】—使用 is null / is not null 对string类型字段进行null值过滤无效

项目场景:

查看测试表test_1,发现表字段classes里面有null值,过滤null值。

--查看
> select * from test_1;
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
| Mary       | class 1         |
| James      | class 2         |
| lily       | null            |
| Mike       | NULL            |
| Herry      | class 1         |
+------------+-----------------+

问题描述

使用where classes is null过滤没有成功。

>  select * from test_1 where classes is null;
>  select * from test_1 where classes is NULL;
>  select * from test_1 where classes is not null;
>  select * from test_1 where classes is not NULL;

--运行结果:
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
+------------+-----------------+

运行的结果都是为空的,并没有将classes为null或者NULL对应的id过滤出来。


原因分析:

使用 is null / is not null 对string类型字段进行过滤无效。

--查看表结构
> desc test_1;

+-----------+------------+----------+
| col_name  | data_type  | comment  |
+-----------+------------+----------+
| id        | string     |          |
| classes   | string     |          |
+-----------+------------+----------+

可以看到classes的类型是string,hive的底层保存的是’null’、'NULL’是个字符串,想要过滤掉null或者NULL值,使用is not null无效。


解决方案:

对于字符串字段,使用 =‘null’,=‘NULL’,!= ‘null’,!= ‘NULL’ 进行过滤。

>  select * from test_1 where classes = 'null';
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
| lily       | null            |
+------------+-----------------+

>  select * from test_1 where classes = 'NULL';
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
| Mike       | NULL            |
+------------+-----------------+

>  select * from test_1 where classes != 'null';
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
| Mary       | class 1         |
| James      | class 2         |
| Mike       | NULL            |
| Herry      | class 1         |
+------------+-----------------+

>  select * from test_1 where classes != 'NULL';
+------------+-----------------+
| test_1.id  | test_1.classes  |
+------------+-----------------+
| Mary       | class 1         |
| James      | class 2         |
| lily       | null            |
| Herry      | class 1         |
+------------+-----------------+
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你要查询的字段是字符串类型,而不是枚举类型,那么就不能使用Hive的内置函数`enum_values()`来查询该字段的所有枚举。这时可以考虑使用UDF函数来实现该功能。 以下是一个示例UDF函数的代码,可以将一个逗号分隔的字符串切分为多个取,并返回这些取的列表: ```java import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.ArrayWritable; import org.apache.hadoop.io.Writable; import org.apache.hadoop.io.WritableComparable; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.hive.serde2.io.HiveArrayWritable; public class GetEnumValues extends UDF { public ArrayWritable evaluate(Text str) { if (str == null) { return null; } String[] values = str.toString().split(","); List<Text> list = new ArrayList<Text>(); for (String value : values) { list.add(new Text(value.trim())); } return new HiveArrayWritable(Text.class, list.toArray(new Writable[list.size()])); } } ``` 将上述代码保存为GetEnumValues.java文件,并编译成GetEnumValues.class文件,然后将其打包成GetEnumValues.jar文件。 在Hive中,使用以下命令将该UDF函数注册到Hive中: ```sql ADD JAR /path/to/GetEnumValues.jar; CREATE TEMPORARY FUNCTION get_enum_values AS 'GetEnumValues'; ``` 然后就可以使用该UDF函数查询一个字符串字段的所有取了。例如: ```sql SELECT explode(get_enum_values(your_string_field)) AS enum_value FROM your_table; ``` 其中,`your_string_field`是你要查询的字符串字段名称,`your_table`是你要查询的表名称。上述SQL语句会返回指定字符串字段的所有取,每个取占据一行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值