我相信您可能想要执行以下操作:
InputDF.where("firstValue >= 0.5 and secondValue >= 0.5 and thirdValue >= 0.5")
您面临的错误是运行时的解析错误,如果错误是由传入的错误类型引起的,它甚至不会编译。
正如您在 official documentation(此处为 Spark 2.3.0 提供)中看到的那样,where 方法可以采用 Columns 序列(就像在您的后一个 sn-p 中一样)或代表一个字符串SQL 谓词(如我的示例)。
SQL 谓词将由 Spark 解释。不过我相信值得一提的是,您可能有兴趣编写 Columns 而不是连接字符串,因为前一种方法通过消除所有可能的错误(例如解析错误)来最小化错误表面。
您可以使用以下代码实现相同的目的:
InputDF.where(col("firstValue") >= 0.5 and col("secondValue") >= 0.5 and col("thirdValue") >= 0.5)
或更简洁:
import spark.implicits._ // necessary for the $"" notation
InputDF.where($"firstValue" >= 0.5 and $"secondValue" >= 0.5 and $"thirdValue" >= 0.5)
Columns 比原始字符串更容易组合并且更健壮。如果您想应用一组条件,您可以轻松地将它们and 放在一个函数中,甚至可以在您运行程序之前进行验证:
def allSatisfied(condition: Column, conditions: Column*): Column =
conditions.foldLeft(condition)(_ and _)
InputDF.where(allSatisfied($"firstValue" >= 0.5, $"secondValue" >= 0.5, $"thirdValue" >= 0.5))
你当然可以用字符串来达到同样的效果,但这最终会变得不那么健壮:
def allSatisfied(condition: String, conditions: String*): String =
conditions.foldLeft(condition)(_ + " and " + _)
InputDF.where(allSatisfied("firstValue >= 0.5", "secondValue >= 0.5", "thirdValue >= 0.5"))