首页 > 解决方案 > 多列工作错误的 Spark 过滤器

问题描述

我有一个包含 30 行和 10 列的数据框。列名是动态变化的。filter此数据框在 2 行的 8 列中有空值,我正在尝试使用带有运算符的 sparkSQL 删除这些记录and

我看到过滤器的奇怪行为。当我用

df.filter('(`1Heavy Buyers` is null) and (`2Non Buyers` is null) and (`3Total Buyers` is null) and (`4Heavy Buyers` is null) and (`7Non Buyers` is null) and (`5Total Buyers` is null) and (`6Heavy Buyers` is null) and (`8Non Buyers` is null) and (`9otal Buyers` is null)')

我找回了 2 条正确的记录。

但是当尝试使用非空行时

df.filter('(`1Heavy Buyers` is not null) and (`2Non Buyers` is not null) and (`3Total Buyers` is not null) and (`4Heavy Buyers` is not null) and (`7Non Buyers` is not null) and (`5Total Buyers` is not null) and (`6Heavy Buyers` is not null) and (`8Non Buyers` is not null) and (`9otal Buyers` is not null)')

我只取回了 16 条记录,应该是 28 条。在这种情况下,即使一列具有空值而不是提到的所有列,过滤器也会删除。

我正在使用火花 2.3.0。

我不明白我在这里做错了什么。

标签: javaapache-sparkapache-spark-sql

解决方案


您运行的第一个查询意味着您打算将所有记录保留在数据框中,其中提到的所有六列的值为空。

但是,在第二个查询中,将保留所有提及的列的值不为空的所有记录。这意味着,即使只有一列具有空值,它也会被过滤掉。要获得您需要的结果,您可以运行以下查询:

df.filter('(`1Heavy Buyers` is not null) or (`2Non Buyers` is not null) or (`3Total Buyers` is not null) or (`4Heavy Buyers` is not null) or (`7Non Buyers` is not null) or (`5Total Buyers` is not null) or (`6Heavy Buyers` is not null) or (`8Non Buyers` is not null) or (`9otal Buyers` is not null)')

上面的查询将为您提供所需的 28 计数。如果您有任何疑问,请随时发表评论。


推荐阅读