【发布时间】:2020-03-17 08:20:21
【问题描述】:
我正在尝试在 scala 中编写 Spark UDF,我需要定义一个函数的输入数据类型
我有一个带有 StructType 的架构变量,如下所述。
import org.apache.spark.sql.types._
val relationsSchema = StructType(
Seq(
StructField("relation", ArrayType(
StructType(Seq(
StructField("attribute", StringType, true),
StructField("email", StringType, true),
StructField("fname", StringType, true),
StructField("lname", StringType, true)
)
), true
), true)
)
)
我正在尝试编写如下所示的函数
val relationsFunc: Array[Map[String,String]] => Array[String] = _.map(do something)
val relationUDF = udf(relationsFunc)
input.withColumn("relation",relationUDF(col("relation")))
上面的代码抛出下面的异常
org.apache.spark.sql.AnalysisException: cannot resolve 'UDF(relation)' due to data type mismatch: argument 1 requires array<map<string,string>> type, however, '`relation`' is of array<struct<attribute:string,email:string,fname:string,lname:string>> type.;;
'Project [relation#89, UDF(relation#89) AS proc#273]
如果我将输入类型指定为
val relationsFunc: StructType => Array[String] =
我无法实现逻辑,因为 _.map 给了我元数据、文件名等。
请建议如何在以下函数中将关系模式定义为输入数据类型。
val relationsFunc: ? => Array[String] = _.map(somelogic)
【问题讨论】:
标签: scala apache-spark apache-spark-sql