记一次 Sedona(GeoSpark) 空间计算优化
项目需求需要空间计算能力,开始选型Sedona(GeoSpark)来完成,
需求需要每一条数据在满足某条件的情况下,去查找某张表进行空间匹配,找到离这个点(point)最近的一条道路(lineString)
第一个方案: 使用sedona来使用临近道路的判断
由于sedona本质还是使用spark的能力,所以遵循spark的开发规则,不能在rdd.map 里面干活,sedona也不支持批量查,只能一条一条匹配。 伪代码如下
val spatial_sql =
"""
| select
| ST_GeomFromWKT(geom) geom, name, adcode
| from ods.ods_third_party_road_data
|""".stripMargin
val third_party_road_df = spark.sql(spatial_sql).toDF()
aoi_day_s_df.rdd.collect().par.map(row => {
val tmp_location = row.getAs[String]("poi_location")
val near_street = spatialQueryStreet(third_party_road_df, city_code, tmp_location)
println(near_street)
...
)
def spatialQueryStreet(third_party_road_df:DataFrame, city_code:String, location: String): String = {
val frame = third_party_road_df.where("adcode = '%s'".format(city_code)).toDF()
val tp_road_spatial_rdd = Adapter.toSpatialRdd(frame, "geom")
tp_road_spatial_rdd.buildIndex(IndexType.RTREE, false)
val geometryFactory = new GeometryFactory()
val x = location.substring(location.indexOf("(") + 1, location.indexOf(" "))
val y = location.substring(location.indexOf(" ") + 1, location.indexOf(")"))
val pointObject = geometryFactory.createPoint(new Coordinate(x.toDouble, y.toDouble))
val usingIndex = true
val result = KNNQuery.SpatialKnnQuery(tp_road_spatial_rdd, pointObject, 1, usingIndex)
if (result.isEmpty) {
return ""
} else {
val dst = result.get(0)
//System.out.println("==== dst.getUserData: " + dst.getUserData.toString)
val strings = dst.getUserData.toString.split("\t")
val near_street = strings(0)
//System.out.println("==== near_street: " + near_street)
near_street
}
结果效率不高,因为每条数据都要匹配,sedona又不能在rdd.map中使用,所以必须先collect().map,这就不能利用到spark多节点并行的特性; 2. 每条数据都基于third_party_road_df创建了空间索引来查,效率更低了(如果只有一条数据还勉强可以接受)
方案2: 改sedona为JTS来处理,jts直接创建rtree,可以在rdd.map中处理,而且创建速度也更快一些,效率更高了
伪代码如下
poi_build_aoi_aoi_day_s_df.rdd.map(row => {
val tmp_location = row.getAs[String]("poi_location")
val rtree = createRtree(model_list)
near_street = spatialQueryStreet(rtree, tmp_location)
println(near_street)
...
)
def createRtree(third_party_road_list: Array[ThirdPartyModel]): STRtree = {
val rtree = new STRtree()
for (model <- third_party_road_list) {
val geom = model.geometry
geom.setUserData(model.name)
rtree.insert(geom.getEnvelopeInternal, model.geometry)
}
rtree.build()
rtree
}
def spatialQueryStreet(rtree: STRtree, location: String): String = {
if (rtree == null) {
""
}
val geometryFactory = new GeometryFactory()
val x = location.substring(location.indexOf("(") + 1, location.indexOf(" "))
val y = location.substring(location.indexOf(" ") + 1, location.indexOf(")"))
val pointObject = geometryFactory.createPoint(new Coordinate(x.toDouble, y.toDouble))
val result = rtree.nearestNeighbour(pointObject.getEnvelopeInternal, pointObject, new GeometryItemDistance())
val name = result.asInstanceOf[Geometry].getUserData.asInstanceOf[String]
println(s"nearestNeighbour name: $name")
name
}
通过这次修改,由原来跑3个小时(甚至更多)的任务在15分钟内就跑完了
PS: 经尝试rtree 不能通过广播变量发送出去,会报序列化异常
其实还可以再优化一下,上面每条数据还是创建了一次rtree, 可以改为mapPartition,然后只建一次rtree, 数据量大时效果更佳
aoi_day_s_df.rdd.mapPartitions(iterator => {
// rtree 放到iterator.map 外面创建,搞一次就ok了,更快(不过我没有试验,应该是百分百可行的)
val rtree = createRtree(model_list)
val seq = iterator.map(row => {
val tmp_location = row.getAs[String]("poi_location")
near_street = spatialQueryStreet(rtree, tmp_location)
println(near_street)
...
)
seq
)
记一次 Sedona(GeoSpark) 空间计算优化的更多相关文章
- 从程序到系统:建立一个更智能的世界——记Joseph Sifakis“21世纪的计算”大会主题演讲
Sifakis"21世纪的计算"大会主题演讲" title="从程序到系统:建立一个更智能的世界--记Joseph Sifakis"21世纪的计算&q ...
- (数据科学学习手札88)基于geopandas的空间数据分析——空间计算篇(下)
本文示例代码及数据已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 在基于geopandas的空间数据分析系列 ...
- Linux启动时间优化-内核和用户空间启动优化实践
关键词:initcall.bootgraph.py.bootchartd.pybootchart等. 启动时间的优化,分为两大部分,分别是内核部分和用户空间两大部分. 从内核timestamp 0.0 ...
- (转) Delete/Truncate删除,释放表空间、降低高水位线、resize释放磁盘空间相关优化
硬盘空间不足,打算删除数据库中的多余数据,但删除数据后,硬盘硬盘空间不能释放.[delete后用:alter table table_name move truncate后用:alter tab ...
- 2019牛客多校第二场F Partition problem 暴力+复杂度计算+优化
Partition problem 暴力+复杂度计算+优化 题意 2n个人分成两组.给出一个矩阵,如果ab两个在同一个阵营,那么就可以得到值\(v_{ab}\)求如何分可以取得最大值 (n<14 ...
- 递归、尾递归和使用Stream延迟计算优化尾递归
我们在学数据结构的时候必然会接触栈(Stack),而栈有一个重要的应用是在程序设计语言中实现递归.递归用途十分广泛,比如我们常见的阶乘,如下代码: 1234 public static int (in ...
- (数据科学学习手札84)基于geopandas的空间数据分析——空间计算篇(上)
本文示例代码.数据及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 在本系列之前的文章中我们主要讨论了g ...
- 记一次 spinor flash 读速度优化
背景 某个项目使用的介质是 spinor, 其 bootloader 需要从 flash 中加载 os. 启动速度是一个关键指标,需要深入优化.其他部分的优化暂且略过,此篇主要记录对 nor 读速度的 ...
- 【算法随记】Canny边缘检测算法实现和优化分析。
以前的博文大部分都写的非常详细,有很多分析过程,不过写起来确实很累人,一般一篇好的文章要整理个三四天,但是,时间越来越紧张,后续的一些算法可能就以随记的方式,把实现过程的一些比较容易出错和有价值的细节 ...
随机推荐
- SAP APO - 简介
SAP高级计划和优化(SAP APO)是SAP SCM中的关键模块之一,在供应链流程中控制供应网络计划,备件计划,TP / VS和需求计划. 它可以帮助组织在动态环境中管理其供应链流程. SAP AP ...
- XML方式配置切面
1. 概述 一个切面中需要包含什么,才能够作用到连接点?切面中是包含通知的,通知作用到连接点需要有切入点表达式. 除了使用AspectJ注解声明切面,Spring也支持在bean配置文件中声明切面. ...
- 如何用Python实现配置热加载?
背景 由于最近工作需求,需要在已有项目添加一个新功能,实现配置热加载的功能.所谓的配置热加载,也就是说当服务收到配置更新消息之后,我们不用重启服务就可以使用最新的配置去执行任务. 如何实现 下面我分别 ...
- STM32与物联网02-网络数据收发
在上一节中,介绍了 ESP8266 的使用方法.不过上一节中都是通过串口调试工具手动发送信息的方式来操作 ESP8266 ,这肯定不能用于实际开发.因此,本节介绍如何编写合适的程序来和 ESP8266 ...
- Airbnb的动态kubernetes集群扩缩容
Airbnb的动态kubernetes集群扩缩容 本文介绍了Airbnb的集群扩缩容的演化历史,以及当前是如何通过Cluster Autoscaler 实现自定义扩展器的.最重要的经验就是Airbnb ...
- 倍增求RMQ
RMQ,即区间最值查询,给定一个序列,求区间l-r的最大值.最小值. st表求RMQ,预处理On*logn,查询O1. 预处理: void init_rmq() { for(rll j=1;j< ...
- 迭代器和增强for循环
iterator 迭代:即Collection集合元素的通过获取方法,在获取元素之前先要判断集合中有没有元素,如果有就把这个元素取出来,然后在判断,如果还有就再去除卡u,一直把集合中的所有元素全部拿出 ...
- java关键字的概念与特征和标识符的概念和规则
什么是关键字 比如说邮箱地址 abc@qq.com 123abc@qq.com 这样的只要没有人占用都是和发布的 那么这样呢 hahah@enen@itcast.cn呢 @是电子邮箱当中有特殊含义的 ...
- std::hash<std::pair<int, int> >
标题是搞笑的 ! 这个问题只需要 since C++11 问题:怎么让 unordered_map 支持使用 pair 作为 key? 如果你能把两个东西压到一个基本类型里那么就不用解决这个问题了 . ...
- mysql常见用法
查看连接数show processlist; 查看慢日志 show variables like '%slow_query_log%'; show variables like 'long_query ...