如果要映射最后一个
case
即
case _
至“无”
String
,则所有案例都应返回
一串
也
下列的
udf
功能应该适合您
def calculateScore = udf((questionId: Int, answerText: String) => (questionId, answerText) match {
case ((31 | 32 | 33 | 34 | 35), "Rarely /<br>Never") => "4" //this is fine
case ((31 | 32 | 33 | 34 | 35), "Occasionally") => "3"
case ((31 | 32 | 33 | 34 | 35), "Often") => "2"
case ((31 | 32 | 33 | 34 | 35), "Almost always /<br>Always") => "1"
case (x, "None of the time") if (x >= 41 && x < 55) => "1" //this line won't compile
case _ => "None"
})
如果要映射最后一个
案例
即
案例_
到
None
,则需要将其他返回类型更改为
Option
像
没有一个
是的孩子
选项
以下代码也适用于您
def calculateScore = udf((questionId: Int, answerText: String) => (questionId, answerText) match {
case ((31 | 32 | 33 | 34 | 35), "Rarely /<br>Never") => Some(4) //this is fine
case ((31 | 32 | 33 | 34 | 35), "Occasionally") => Some(3)
case ((31 | 32 | 33 | 34 | 35), "Often") => Some(2)
case ((31 | 32 | 33 | 34 | 35), "Almost always /<br>Always") => Some(1)
case (x, "None of the time") if (x >= 41 && x < 55) => Some(1) //this line won't compile
case _ => None
})
最后一点是您收到的错误消息
java.lang.UnsupportedOperationException: Schema for type Any is not supported
明确指出:
自定义项
返回类型为的函数
Any
不支持。所有的
return types
从
match cases
应保持一致。