为什么简单的UDF会引发不受支持的错误?

时间:2019-04-22 19:00:05

标签: apache-spark apache-spark-sql user-defined-functions

仅使用UDF函数连接2个字符串..以下是我的代码

import  org.apache.spark.sql.functions.udf
val udfconcat =udf[String ,String, String](concat_udf)

spark.sqlContext.udf.register("udf_sel","udfconcat")

spark.sql("select FirstName,LastName, udf_sel(FirstName,LastName) AS CONCATENATEDNAME FROM checks").show()

def concat_udf(fN: String,lN : String) : String ={
    fN + lN
}

我的模式是

   EmployeeID|      int|   null|
|   FirstName|   string|   null|
|    LastName|   string|   null|
|DepartmentID|   bigint|   null|
|      Salary|   double|   null|

当我尝试执行时,我会得到

  

不支持Char类型的模式

1 个答案:

答案 0 :(得分:0)

您在注册udf时传递了 String,String ,但是要注册UDF,我们需要将args传递为 Stringfunction < / p>

  • 在这种情况下,我们需要通过 "udf_sel",concat_udf _

尝试使用以下任何一种方法:

def concat_udf(fN: String,lN : String) : String ={
    fN + lN
}

spark.sqlContext.udf.register("udf_sel",concat_udf _)

spark.sql("select FirstName,LastName, udf_sel(FirstName,LastName) AS CONCATENATEDNAME FROM checks").show()

(或)

val cnct=(fn:String,ln:String)=> {fn + ln}

spark.sqlContext.udf.register("udf_sel",cnct)

spark.sql("select FirstName,LastName, udf_sel(FirstName,LastName) AS CONCATENATEDNAME FROM checks").show()

(或)

spark.sqlContext.udf.register("udf_sel",(fn:String,ln:String) => fn+ln)

spark.sql("select FirstName,LastName, udf_sel(FirstName,LastName) AS CONCATENATEDNAME FROM checks").show()