有没有一种方法可以在GeoSpark中有效地对spatialRDD进行空间分区?例如:使用GeoSpark或类似的方法将多个点接近的分区放在一个分区中?
发布于 2019-07-29 19:49:32
作为Georg comment的一个扩展,我想向您展示一个使用QuadTree的示例。我没有使用其余的分区方法,但我希望它们的行为是相同的(当然,除了实际的分区)。假设您想要分区的变量是pointsRDD (在我的例子中,它实际上是一个PointRDD类型的对象),您可以按以下方式进行分区:
import com.vividsolutions.jts.index.quadtree.Quadtree
import com.vividsolutions.jts.index.SpatialIndex
val buildOnSpatialPartitionedRDD = true // Set to TRUE only if run join query
val numPartitions = 48
pointsRDD.analyze()
pointsRDD.spatialPartitioning(GridType.QUADTREE, numPartitions)
pointsRDD.buildIndex(IndexType.QUADTREE, buildOnSpatialPartitionedRDD)您将在pointsRDD.spatialPartitionedRDD.rdd中找到分区的数据
pointsRDD
.spatialPartitionedRDD
.rdd
.mapPartitions(yourFunctionYouWantToRunOnEachPartition)您可以通过查看分区树来检查分区:
pointsRDD.partitionTree.getAllZones.asScala.foreach(println)这将产生类似于
x: 15.857028 y: 53.36364 w: 9.872338000000003 h: 2.7383549999999985 PartitionId: null Lineage: null
x: 15.857028 y: 54.732817499999996 w: 4.936169000000001 h: 1.3691774999999993 PartitionId: null Lineage: null
x: 15.857028 y: 55.41740625 w: 2.4680845000000007 h: 0.6845887499999996 PartitionId: null Lineage: null
x: 15.857028 y: 55.759700625 w: 1.2340422500000003 h: 0.3422943749999998 PartitionId: null Lineage: null
x: 15.857028 y: 55.9308478125 w: 0.6170211250000002 h: 0.1711471874999999 PartitionId: 0 Lineage: null
...这可以用你最喜欢的绘图工具可视化(不能包含代码,抱歉):

要检查分区统计信息,请使用以下代码:
import org.apache.spark.sql.functions._
pointsRDD
.spatialPartitionedRDD
.rdd
.mapPartitionsWithIndex{case (i,rows) => Iterator((i,rows.size))}
.toDF("partition_number","number_of_records")
.show()这将为您提供:
+----------------+-----------------+
|partition_number|number_of_records|
+----------------+-----------------+
| 0| 8240|
| 1| 7472|
| 2| 5837|
| 3| 3753|
+----------------+-----------------+
only showing top 4 rows发布于 2018-06-03 22:51:37
请参阅http://datasystemslab.github.io/GeoSpark/tutorial/rdd/#use-spatial-partitioning
都是实现的
https://stackoverflow.com/questions/50065930
复制相似问题