gpt4 book ai didi

json - Graphx 中使用 Spark 的最短路径性能

转载 作者:行者123 更新时间:2023-12-03 17:38:50 28 4
gpt4 key购买 nike

我正在从 gz 创建一个图表压缩 json edge 的文件和 vertices类型。

我已将文件放在 dropbox 文件夹中 here

我加载并映射这些 json记录创建 verticesedge graphx 所需的类型像这样:

val vertices_raw = sqlContext.read.json("path/vertices.json.gz")
val vertices = vertices_raw.rdd.map(row=> ((row.getAs[String]("toid").stripPrefix("osgb").toLong),row.getAs[Long]("index")))
val verticesRDD: RDD[(VertexId, Long)] = vertices
val edges_raw = sqlContext.read.json("path/edges.json.gz")
val edgesRDD = edges_raw.rdd.map(row=>(Edge(row.getAs[String]("positiveNode").stripPrefix("osgb").toLong, row.getAs[String]("negativeNode").stripPrefix("osgb").toLong, row.getAs[Double]("length"))))
val my_graph: Graph[(Long),Double] = Graph.apply(verticesRDD, edgesRDD).partitionBy(PartitionStrategy.RandomVertexCut)

然后我用这个 dijkstra我发现计算两个顶点之间的最短路径的实现:
def dijkstra[VD](g: Graph[VD, Double], origin: VertexId) = {
var g2 = g.mapVertices(
(vid, vd) => (false, if (vid == origin) 0 else Double.MaxValue, List[VertexId]())
)
for (i <- 1L to g.vertices.count - 1) {
val currentVertexId: VertexId = g2.vertices.filter(!_._2._1)
.fold((0L, (false, Double.MaxValue, List[VertexId]())))(
(a, b) => if (a._2._2 < b._2._2) a else b)
._1

val newDistances: VertexRDD[(Double, List[VertexId])] =
g2.aggregateMessages[(Double, List[VertexId])](
ctx => if (ctx.srcId == currentVertexId) {
ctx.sendToDst((ctx.srcAttr._2 + ctx.attr, ctx.srcAttr._3 :+ ctx.srcId))
},
(a, b) => if (a._1 < b._1) a else b
)
g2 = g2.outerJoinVertices(newDistances)((vid, vd, newSum) => {
val newSumVal = newSum.getOrElse((Double.MaxValue, List[VertexId]()))
(
vd._1 || vid == currentVertexId,
math.min(vd._2, newSumVal._1),
if (vd._2 < newSumVal._1) vd._3 else newSumVal._2
)
})
}

g.outerJoinVertices(g2.vertices)((vid, vd, dist) =>
(vd, dist.getOrElse((false, Double.MaxValue, List[VertexId]()))
.productIterator.toList.tail
))
}

我取两个随机顶点 ID:
val v1 = 4000000028222916L
val v2 = 4000000031019012L

并计算它们之间的路径:
val results = dijkstra(my_graph, v1).vertices.map(_._2).collect

我无法在我的笔记本电脑上本地计算它而不会出现 stackoverflow 错误。我可以看到它使用了 4 个可用内核中的 3 个。我可以加载此图并使用 igraph 计算每秒最短的 10 条路径Python 中的库在完全相同的图上。这是计算路径的低效方法吗?在规模上,在多个节点上将计算路径(没有计算器溢出错误),但每个路径计算仍然是 30/40 秒。

最佳答案

正如您在 python-igraph github 上所读到的那样

"It is intended to be as powerful (ie. fast) as possible to enable the analysis of large graphs."



为了解释为什么在 apache-spark 上花费的时间比在本地 python 上多 4000 倍,你可以看看 here (与 Spark PMC 成员 Kay Ousterhout 一起深入研究性能瓶颈。)看到它可能是由于瓶颈造成的:

... beginning with the idea that network and disk I/O are major bottlenecks ... You may not need to store your data in-memory because the job may not get that much faster. This is saying that if you moved the serialized compressed data from on-disk to in-memory...



你也可以看到 here & here一些信息,但最好的 final方法是对代码进行基准测试以了解瓶颈在哪里。

关于json - Graphx 中使用 Spark 的最短路径性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41574474/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com