我正在尝试编写通用的add方法,该方法可以采用任何参数类型并返回该类型的结果
def addExactUDF[T](x: T, y: T)(implicit num: Numeric[T]): T = {
import num._
x + y
}
def addExact(value1: Column, value2: Column, dataType: String): Column =
dataType match {
case "Int" => expr(s"addExactUDF(cast($value1 AS INT), cast($value2 AS INT))")
case "Double" => expr(s"addExactUDF(cast($value1 AS DOUBLE), cast($value2 AS DOUBLE))")
}
现在当我尝试注册UDF时
object FilterFunctionsUtil extends MathFunctionsNameSpace with StringFunctionsNameSpace {
lazy val registerAsSparkUdf: UserDefinedFunction = {
val sqlContext = SparkSessionFactory.getSparkSession(Map(), Nil).sqlContext
sqlContext.udf.register("addExactUDF", addExactUDF _)
sqlContext.udf.register("subtractExactUDF", subtractExactUDF _)
}
}
这让我出错
Error:(36, 44) could not find implicit value for parameter num: Numeric[Nothing]
sqlContext.udf.register("addExactUDF", addExactUDF _)
Error:(36, 44) not enough arguments for method addExactUDF: (implicit num: Numeric[Nothing])Nothing.
Unspecified value parameter num.
sqlContext.udf.register("addExactUDF", addExactUDF _)
我如何添加隐式函数以使其正常工作
答案 0 :(得分:3)
在注册UDF时指定隐式类型。您可能需要根据需要为每种类型分别注册UDF。
import sqlContext.implicits._
val df = sc.parallelize(Seq((1,2),(3,4))).toDF("val1","val2")
df.createOrReplaceTempView("tempTable")
sqlContext.cacheTable("tempTable")
sqlContext.udf.register("addExtractIntUDF",addExtractUDF[Int] _)
sqlContext.sql("select addExtractIntUDF(val1,val2) from temptable").show(false)
结果
+ --------------------------------------------- +
| UDF:addExtractIntUDF(val1,val2)|
+ --------------------------------------------- +
| 3的| 3的
| | | | | | 7 |
+ -------------------------------------------- +