检查arraytype列是否包含null [英] Check if arraytype column contains null

查看:117
本文介绍了检查arraytype列是否包含null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中的一列arraytype可以包含整数值.如果没有值,它将仅包含一个,并且将为空值

I have a dataframe with a column of arraytype that can contain integer values. If no values it will contain only one and it will be the null value

重要:请注意,该列将不会为空,而是一个具有单个值的数组;空

Important: note the column will not be null but an array with a single value; null

> val df: DataFrame  = Seq(("foo", Seq(Some(2), Some(3))), ("bar", Seq(None))).toDF("k", "v")
df: org.apache.spark.sql.DataFrame = [k: string, v: array<int>]
> df.show()
+---+------+
|  k|     v|
+---+------+
|foo|[2, 3]|
|bar|[null]|

问题:我想获取具有空值的行.

Question: I'd like to get the rows that have the null value.

感谢您的帮助

到目前为止我已经尝试过的:

What I have tried thus far:

> df.filter(array_contains(df("v"), 2)).show()
+---+------+
|  k|     v|
+---+------+
|foo|[2, 3]|
+---+------+

对于null,它似乎不起作用

for null, it does not seem to work

> df.filter(array_contains(df("v"), null)).show()
org.apache.spark.sql.AnalysisException: cannot resolve 'array_contains(`v`, NULL)' due to data type mismatch: Null typed values cannot be used as arguments;

> df.filter(array_contains(df("v"), None)).show()
java.lang.RuntimeException: Unsupported literal type class scala.None$ None

推荐答案

在这种情况下,由于无法对SQL NULL进行相等性比较,因此无法使用array_contains.

It is not possible to use array_contains in this case because SQL NULL cannot be compared for equality.

您可以像这样使用udf:

val contains_null = udf((xs: Seq[Integer]) => xs.contains(null))

df.where(contains_null($"v")).show

// +---+------+
// |  k|     v|
// +---+------+
// |bar|[null]|

这篇关于检查arraytype列是否包含null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆