You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spark DataFrame筛选非空行报错排查与正确实现

Spark DataFrame 筛选非空列报错的排查与解决

哈哈,一眼就看出问题所在啦!咱们先拆解下报错原因:

报错根源

你写的代码里括号位置出了问题:

df_alerte.filter(df_alerte.col("heureEntre").isNotNull().and(df_alerte.col("heureSortie")).isNotNull()).show(1000);

这里的.and(df_alerte.col("heureSortie"))是把字符串类型的列对象直接传给了逻辑与方法,而不是传入isNotNull()返回的布尔表达式。Spark会把heureEntre IS NOT NULL的布尔结果和heureSortie的字符串值做AND运算,这就触发了boolean and string的类型不匹配错误,也就是报错里提到的differing types in '((heureEntreIS NOT NULL) ANDheureSortie)' (boolean and string)。

正确实现方式

我们只需要修正括号位置,确保.and()的参数是另一个isNotNull()的布尔表达式即可:

方式一:使用列对象API

df_alerte.filter(
    df_alerte.col("heureEntre").isNotNull()
    .and(df_alerte.col("heureSortie").isNotNull())
).show(1000);

或者用更清晰的链式写法(提前导入col函数):

import org.apache.spark.sql.functions.col;

df_alerte.filter(col("heureEntre").isNotNull().and(col("heureSortie").isNotNull()))
         .show(1000);

方式二:使用SQL字符串表达式(更简洁)

如果你习惯SQL语法,直接写字符串表达式会更直观:

df_alerte.filter("heureEntre IS NOT NULL AND heureSortie IS NOT NULL")
         .show(1000);

也可以用where方法(和filter功能完全等价):

df_alerte.where("heureEntre IS NOT NULL AND heureSortie IS NOT NULL")
         .show(1000);

这样就能正确筛选出heureEntre和heureSortie两列都不为空的数据行啦!

内容的提问来源于stack exchange,提问作者user13117513

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 07:47:42