具有多个条件时的 Spark Scala 案例

Spark Scala case when with multiple conditions

我正在尝试在我拥有的 DF 上做一个案例,但我遇到了错误。我想用内置的 spark 函数实现这个 - withcolumn, when, otherwise:

CASE WHEN vehicle="BMW" 
AND MODEL IN ("2020","2019","2018","2017") 
AND value> 100000 THEN 1
ELSE 0 END AS NEW_COLUMN

目前我有这个

DF.withColumn(NEW_COLUMN, when(col(vehicle) === "BMW" 
and col(model) isin(listOfYears:_*) 
and col(value) > 100000, 1).otherwise(0))

但是由于数据类型不匹配(布尔值和字符串),我收到了一个错误...我理解我的情况 returns 布尔值和字符串,这是导致错误的原因。执行这种情况的正确语法是什么?另外,我使用 && 而不是 and 但是第三个 && 给了我一个“无法解析符号 &&”

感谢您的帮助!

我认为 && 是正确的 - 使用内置的 spark 函数,所有表达式的类型都是 Column, checking the API it looks like && is correct and should work fine. Could it be as simple as an order-of-operations issue, where you need parentheses around each of the boolean conditions? The function / "operator" isin would have a lower precedence 而不是 &&,这可能会出错。