找不到org.locationtech.jts.geom.Point的编码器

时间:2019-03-06 22:51:22

标签: scala apache-spark jts geomesa

在使用Geomesa和Scala时,我一直在尝试使用以下代码片段在Spark Dataframe中编码2列,但我不断收到一个问题,似乎Scala无法将返回的对象序列化为Dataframe。使用Postgres和PostGIS时,生活很容易-是一个容易的问题,还是有一个更好的库可以处理来自Double Data格式的经纬度和经度的Spark数据框的地理空间查询?

我在SBT中使用的版本是:

  • 火花:2.3.0
  • 斯卡拉:2.11.12
  • geomesa:2.2.1
  • jst- *:1.17.0-快照
  

线程“ main”中的异常java.lang.UnsupportedOperationException:找不到用于org.locationtech.jts.geom.Point的编码器

import org.apache.spark.sql.SparkSession
import org.locationtech.jts.geom.{Coordinate, GeometryFactory}
import org.apache.spark.sql.functions.col

import org.apache.spark.sql.types._
import org.locationtech.geomesa.spark.jts._


object GetRandomData {


  def main(sysArgs: Array[String]) {

    @transient val spark: SparkSession = {
      SparkSession
        .builder()
        .config("spark.ui.enabled", "false")
        .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
        .config("spark.kryoserializer.buffer.mb","24")
        .appName("GetRandomData")
        .master("local[*]")
        .getOrCreate()
    }
    val sc = spark.sparkContext
    sc.setLogLevel("ERROR")
    import spark.sqlContext.implicits._

    var coordinates = sc.parallelize(
      List(
        (35.40466, -80.905458),
        (35.344079, -80.872267),
        (35.139606, -80.840845),
        (35.537786, -80.780051),
        (35.525361, -83.031932),
        (34.928323, -80.766732),
        (35.533865, -82.72344),
        (35.50997,  -80.588572),
        (35.286251, -83.150514),
        (35.558519, -81.067069),
        (35.569311, -80.916993),
        (35.835867, -81.067904),
        (35.221695, -82.662141)
      )
    ).
    toDS().
    toDF("geo_lat", "geo_lng")

    coordinates = coordinates.select(coordinates.columns.map(c => col(c).cast(DoubleType)) : _*)
    coordinates.show()
    val testing = coordinates.map(r => new GeometryFactory().createPoint(new Coordinate(3.4, 5.6)))
    val coordinatesPointDf = coordinates.withColumn("point", st_makePoint(col("geo_lat"), col("geo_lng")))

  }
}

例外是:

    Exception in thread "main" java.lang.UnsupportedOperationException: No Encoder found for org.locationtech.jts.geom.Point
- root class: "org.locationtech.jts.geom.Point"
  at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$org$apache$spark$sql$catalyst$ScalaReflection$$serializerFor$1.apply(ScalaReflection.scala:643)
  at org.apache.spark.sql.catalyst.ScalaReflection$$anonfun$org$apache$spark$sql$catalyst$ScalaReflection$$serializerFor$1.apply(ScalaReflection.scala:445)
  at scala.reflect.internal.tpe.TypeConstraints$UndoLog.undo(TypeConstraints.scala:56)
  at org.apache.spark.sql.catalyst.ScalaReflection$class.cleanUpReflectionObjects(ScalaReflection.scala:824)
  at org.apache.spark.sql.catalyst.ScalaReflection$.cleanUpReflectionObjects(ScalaReflection.scala:39)
  at org.apache.spark.sql.catalyst.ScalaReflection$.org$apache$spark$sql$catalyst$ScalaReflection$$serializerFor(ScalaReflection.scala:445)
  at org.apache.spark.sql.catalyst.ScalaReflection$.serializerFor(ScalaReflection.scala:434)
  at org.apache.spark.sql.catalyst.encoders.ExpressionEncoder$.apply(ExpressionEncoder.scala:71)
  at org.locationtech.geomesa.spark.jts.encoders.SpatialEncoders$class.jtsPointEncoder(SpatialEncoders.scala:21)
  at org.locationtech.geomesa.spark.jts.package$.jtsPointEncoder(package.scala:17)
  at GetRandomData$.main(Main.scala:50)
  at GetRandomData.main(Main.scala)

2 个答案:

答案 0 :(得分:2)

如果您不使用底层的GeoMesa存储将数据加载到spark会话中,则需要使用以下命令显式注册JTS类型:

org.apache.spark.sql.SQLTypes.init(spark.sqlContext)

这将注册ST_操作以及JTS编码器。

答案 1 :(得分:1)

用简单的英语说:

  

我不知道如何将Point转换为Spark类型。

如果在数据集中将纬度和经度保持为两倍,那应该没问题,但是一旦使用Point之类的对象,就需要告诉Spark如何进行转换。用Spark术语来说,它们称为编码器,您可以创建自定义编码器。

或者您切换到RDD,只要您不介意丢失Spark SQL内容,就无需进行转换。