【问题标题】:Spark 2.1.0 UDF Schema type not supported不支持 Spark 2.1.0 UDF 模式类型
【发布时间】:2017-09-24 13:00:29
【问题描述】:

我正在使用一种称为 Point(x: Double, y: Double) 的数据类型。我正在尝试使用 _c1 和 _c2 列作为 Point() 的输入,然后创建一个新的 Point 值列,如下所示

val toPoint = udf{(x: Double, y: Double) => Point(x,y)}

然后我调用函数:

val point = data.withColumn("Point", toPoint(watned("c1"),wanted("c2")))

但是,当我声明 udf 时,我收到以下错误:

java.lang.UnsupportedOperationException: Schema for type com.vividsolutions.jts.geom.Point is not supported
      at org.apache.spark.sql.catalyst.ScalaReflection$.schemaFor(ScalaReflection.scala:733)
      at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$schemaFor$2.apply(ScalaReflection.scala:729)
      at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$schemaFor$2.apply(ScalaReflection.scala:728)
      at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
      at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
      at scala.collection.immutable.List.foreach(List.scala:381)
      at scala.collection.TraversableLike$class.map(TraversableLike.scala:234)
      at scala.collection.immutable.List.map(List.scala:285)
      at org.apache.spark.sql.catalyst.ScalaReflection$.schemaFor(ScalaReflection.scala:728)
      at org.apache.spark.sql.catalyst.ScalaReflection$.schemaFor(ScalaReflection.scala:671)
      at org.apache.spark.sql.functions$.udf(functions.scala:3084)
      ... 48 elided

我已经正确导入了这种数据类型,并且之前使用过很多次。现在我尝试将它包含在我的 udf 的架构中,它无法识别它。除了标准的 Int、String、Array 等类型之外,还有什么方法包含...

我在 Amazon EMR 上使用 Spark 2.1.0。

这里有一些我参考过的相关问题:

How to define schema for custom type in Spark SQL?

Spark UDF error - Schema for type Any is not supported

【问题讨论】:

  • 在您的示例中wanted() 是什么?
  • @himanshulllTian 抱歉,这是包含 c1、c2、c3 等列的数据库。
  • 你考虑过接受我的回答吗?

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


【解决方案1】:

您应该将Point 定义为案例类

case class Point(x: Double, y: Double)

或者如果你愿意

case class MyPoint(x:Double,y:Double) extends com.vividsolutions.jts.geom.Point(x,y)

这样,Spark 会自动推断架构

【讨论】:

  • 这样做我得到以下错误:case class myPoint has case ancestor geotrellis.vector.Point, but case-to-case inheritance is prohibited. To overcome this limitation, use extractors to pattern match on non-leaf nodes.
猜你喜欢
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多