[英]Spark UDF return type
I am trying to make a UDF like so: 我正在尝试制作这样的UDF:
import org.apache.spark.sql.functions.udf
def myFunction = udf( (input: String, modifier: Seq[String]) => {
// Logic goes here...
return Option(myString)
})
The function has a few exit points in the logic, but it complains about the return statement as per: 该函数在逻辑上有一些退出点,但是它按照以下方式抱怨return语句:
Message: <console>:69: error: method myFunction has return statement; needs result type
return Option(myString)
I have spent the last few hours trying to work out how to declare the return type as an Option[String] but nothing seems to work. 我花了最后几个小时尝试找出如何将返回类型声明为Option [String],但似乎没有任何效果。
I thought this would have been right, but it doesnt like it: 我认为这是正确的,但它不喜欢它:
udf( (input: String, modifier: Seq[String]) => Option[String] {
I feel like I sort of resolved this. 我觉得我已经解决了这个问题。
As Ramesh pointed out, you dont have to use the return key word in a UDF. 正如Ramesh指出的那样,您不必在UDF中使用返回关键字。 I had done this initially as there was a pile of IF-THEN-ELSE sort of blocks and I wanted the option to exit the function earlier with a return value. 我最初是这样做的,因为有一堆IF-THEN-ELSE类型的块,并且我希望该选项可以更早地退出并返回一个值。
I've now updated this: 我现在更新了这个:
def myFunction = udf( (input: String, modifier: Seq[String]) => Option[String] {
var retrunStr = ""
// Logic goes here...
// Conditionally set a value
retrunStr = input
// Finally just set the result to the returnStr
retrunStr
})
This seems to work, although it does feel messy to carry the returnStr variable. 尽管携带returnStr变量确实感到混乱,但这似乎可行。 If I was really good, I should be able to get my conditional logic to return the correct value. 如果我真的很厉害,我应该能够得到我的条件逻辑来返回正确的值。
I removed some of the complexity in the logic, but I'm using nested matchers and foreach loops and breakables etc. but thats another topic. 我消除了逻辑上的一些复杂性,但是我使用的是嵌套匹配器,foreach循环和易碎对象等,但这是另一个主题。
Thanks again Ramesh, you were right to avoid using a return statement at all. 再次感谢Ramesh,您完全可以避免使用return语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.