gpt4 book ai didi

scala - 将 StructType 定义为函数 Spark-Scala 2.11 的输入数据类型

转载 作者:行者123 更新时间:2023-12-04 01:37:34 24 4
gpt4 key购买 nike

<分区>

我正在尝试在 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 给我元数据、文件名等。

请建议如何在下面的函数中将 relationsSchema 定义为输入数据类型。

val relationsFunc: ? => Array[String] = _.map(somelogic)

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com