是否可以在Spark UDF中使用Option [英] Is it possible to use Option with spark UDF
本文介绍了是否可以在Spark UDF中使用Option的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 Option
作为函数的输入类型.
I'd like to use Option
as input type for my functions.
udf((oa:Option [String],ob:Option [String]))=>…
以更实用的方式处理 null
值.
to handle null
values in a more functional way.
有没有办法做到这一点?
Is there a way to do that ?
推荐答案
据我所知,这不是直接可能的.没有什么能阻止您使用 Options
:
As far as I know it is not directly possible. Nothing stops you wrapping arguments with Options
:
udf((oa: String, ob: String) => (Option(oa), Option(ob)) match {
...
})
使用数据集
编码器:
val df = Seq(("a", None), ("b", Some("foo"))).toDF("oa", "ob")
df.as[(Option[String], Option[String])]
或添加一些隐式转换:
implicit def asOption[T](value: T) : Option[T] = Option(value)
def foo(oa: Option[String], ob: Option[String]) = {
oa.flatMap(a => ob.map(b => s"$a - $b"))
}
def wrap[T, U, V](f: (Option[T], Option[U]) => V) =
(t: T, u: U) => f(Option(t), Option(u))
val foo_ = udf(wrap(foo))
df.select(foo_($"oa", $"ob"))
这篇关于是否可以在Spark UDF中使用Option的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文