如何处理原始可为空类型的Spark UDF输入/输出 [英] How to deal with Spark UDF input/output of primitive nullable type
本文介绍了如何处理原始可为空类型的Spark UDF输入/输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题:
1)如果输入是包含null
的原始类型的列,Spark不会调用UDF:
1) Spark doesn't call UDF if input is column of primitive type that contains null
:
inputDF.show()
+-----+
| x |
+-----+
| null|
| 1.0|
+-----+
inputDF
.withColumn("y",
udf { (x: Double) => 2.0 }.apply($"x") // will not be invoked if $"x" == null
)
.show()
+-----+-----+
| x | y |
+-----+-----+
| null| null|
| 1.0| 2.0|
+-----+-----+
2)无法从UDF生成null
作为原始类型的列:
2) Can't produce null
from UDF as a column of primitive type:
udf { (x: String) => null: Double } // compile error
推荐答案
查看全文