我有一个数据框,它有一个复杂的列数据类型 Arraytype>。为了转换这个数据框,我创建了 udf,它可以使用 Array [case class] 作为参数来使用这个列。这里的主要瓶颈是当我根据 stucttype 创建 case 类时,structfield 名称包含特殊字符,例如“##field”。因此,我以这种方式为 case class ( ##field
)提供相同的名称,并将其附加到 udf 参数。在 spark udf 定义中解释后,将案例类字段的名称更改为此“$hash$hashfield”。使用此数据帧执行转换时,由于此未命中匹配而失败。请帮忙 ...
由于 JVM 限制,Scala 以编码形式存储标识符,目前 Spark 无法映射##field
到$hash$hashfield
.
一种可能的解决方案是从原始行中手动提取字段(但您需要知道 中字段的顺序df
,您可以使用df.schema
它):
val myUdf = udf { (struct: Row) =>
// Pattern match struct:
struct match {
case Row(a: String) => Foo(a)
}
// .. or extract values from Row
val `##a` = struct.getAs[String](0)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句