你不需要
udf
功能。简单的嵌套
when
内置函数应该起作用。
import org.apache.spark.sql.functions._
df.withColumn("CODE", col("CODE").cast("string"))
.withColumn("flag", when(((isnull(col("CODE")) || col("CODE") === "null") && (isnull(col("revised_code")) || col("revised_code") === "null")), "").otherwise(when(col("CODE") === col("revised_code"), "P").otherwise("I")))
.show(false)
在这里,
CODE
柱子铸成
stringType
在逻辑应用之前使用when,以便
代码
和
revised_code
比较时在数据类型中匹配。
注:
代码
列是
IntegerType
在任何情况下都不能为空。