我一直在用简单的空间查询测试geomesa,并将其与Postgis进行比较。例如,在Postgis中,此SQL查询在30秒内运行:
with series as (
select
generate_series(0, 5000) as i
),
points as (
select ST_Point(i, i*2) as geom from series
)
select st_distance(a.geom, b.geom) from points as a, points as b现在,以下geomesa版本需要5分钟(使用-Xmx10g ):
import org.apache.spark.sql.SparkSession
import org.locationtech.geomesa.spark.jts._
import org.locationtech.jts.geom._
object HelloWorld {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder
.config("spark.sql.crossJoin.enabled", "true")
.config("spark.executor.memory", "12g")
.config("spark.driver.memory", "12g")
.config("spark.cores.max", "4")
.master("local")
.appName("Geomesa")
.getOrCreate()
spark.withJTS
import spark.implicits._
val x = 0 until 5000
val y = for (i <- x) yield i*2
val coords = for ((i, n) <- x.zipWithIndex) yield (i, y(n))
val points = for (i <- coords) yield new GeometryFactory().createPoint(new Coordinate(i._1, i._2))
val points2 = for (i <- coords) yield new GeometryFactory().createPoint(new Coordinate(i._1, i._2))
val all_points = for {
i <- points
j <- points2} yield (i, j)
val df = all_points.toDF("point", "point2")
val df2 = df.withColumn("dist", st_distance($"point", $"point2"))
df2.show()
}
}我期望geomesa有类似或更好的性能,可以做些什么来调优这样的查询?
第一次编辑
正如埃米利奥所建议的那样,这不是一个真正的查询,而是一个计算。这个查询可以在没有spark的情况下编写。下面的代码在不到两秒内运行:
import org.locationtech.jts.geom._
object HelloWorld {
def main(args: Array[String]): Unit = {
val x = 0 until 5000
val y = for (i <- x) yield i*2
val coords = for ((i, n) <- x.zipWithIndex) yield (i, y(n))
val points = for (i <- coords) yield new GeometryFactory().createPoint(new Coordinate(i._1, i._2))
val points2 = for {
i <- points
j <- points} yield i.distance(j)
println(points2.slice(0,30))
}
}发布于 2019-12-05 04:14:22
对于少量数据,GeoMesa不会像PostGIS那样快。GeoMesa是为分布式NoSQL数据库设计的。如果您的数据集适合PostGIS,那么您可能应该只使用PostGIS。一旦你达到了PostGIS的极限,你就应该考虑使用GeoMesa。GeoMesa确实提供了与任意GeoTools数据存储(包括PostGIS)的集成,这可以使一些GeoMesa Spark和command-line功能可用于PostGIS。
对于您的特定代码片段,我怀疑大部分时间都花在启动RDD和遍历循环上。这并不是真正的“查询”,因为你只是在运行成对计算。如果您正在查询表中存储的数据,那么GeoMesa有机会优化扫描。然而,GeoMesa不是SQL数据库,并且没有任何对连接的原生支持。一般来说,连接是由Spark在内存中完成的,尽管你可以做一些事情来加速它(例如broadcast join或RDD partitioning)。如果您想要进行复杂的空间连接,您可能需要查看GeoSpark和/或Magellan,它们专门从事空间Spark操作。
https://stackoverflow.com/questions/59126696
复制相似问题