选项我有一个数据集是这样的:如何使用星火UDF
+----+------+
|code|status|
+-----------+
| 1| "new"|
| 2| null|
| 3| null|
+----+------+
我想编写依赖于两列的UDF。
我得到它的工作按照this answer第二种方法是处理null
的UDF之外,写myFn
采取布尔作为第二个参数:
df.withColumn("new_column",
when(pst_regs("status").isNull,
myFnUdf($"code", lit(false))
)
.otherwise(
myFnUdf($"code", lit(true))
)
)
要在UDF处理空的方法我看着是this answer,谈论“用Options
包装参数”。我想这样的代码:
df.withColumn("new_column", myFnUdf($"code", $"status"))
def myFn(code: Int, status: String) = (code, Option(status)) match {
case (1, "new") => "1_with_new_status"
case (2, Some(_)) => "2_with_any_status"
case (3, None) => "3_no_status"
}
但随着null
一个行给出type mismatch; found :None.type required String
。我也尝试在udf创建期间用Option
包装参数而没有成功。这个(没有选项)的基本形式如下:
myFnUdf = udf[String, Int, String](myFn(_:Int, _:String))
我是新来的Scala,所以我敢肯定,我失去了一些东西简单。我的一些混淆可能是从功能创建udfs的不同语法(例如,根据https://jaceklaskowski.gitbooks.io/mastering-apache-spark/content/spark-sql-udfs.html),所以我不确定我是否使用了最好的方法。任何帮助感谢!
编辑
编辑补充缺少的每@ user6910411和@sgvd评论(1, "new")
情况。
谢谢@sgvd。我使用这两种方法(并更新了问题以包含丢失的案例)。感谢你的帮助。 –