【发布时间】:2018-12-04 20:01:49
【问题描述】:
考虑这个图表:
如何从一个 vertexID 获取所有连接的顶点?
例如,来自VertexId 5,它应该返回5-3-7-8-10
CollectNeighbors 只返回第一个相邻的。
我正在尝试使用pregel,但我不知道如何从特定顶点开始。我不想计算所有节点。
谢谢!
【问题讨论】:
标签: scala apache-spark spark-graphx
考虑这个图表:
如何从一个 vertexID 获取所有连接的顶点?
例如,来自VertexId 5,它应该返回5-3-7-8-10
CollectNeighbors 只返回第一个相邻的。
我正在尝试使用pregel,但我不知道如何从特定顶点开始。我不想计算所有节点。
谢谢!
【问题讨论】:
标签: scala apache-spark spark-graphx
我刚刚注意到图表是有向的。那么您可以使用最短路径示例的代码here。如果特定节点的距离不是无穷大,那么您可以到达该节点。
或者有更好的想法,您可以修改最短路径算法以满足您的需求。
import org.apache.spark.graphx.{Graph, VertexId}
import org.apache.spark.graphx.util.GraphGenerators
// A graph with edge attributes containing distances
val graph: Graph[Long, Double] =
GraphGenerators.logNormalGraph(sc, numVertices = 100).mapEdges(e => e.attr.toDouble)
val sourceId: VertexId = 42 // The ultimate source
// Initialize the graph such that all vertices except the root have canReach = false.
val initialGraph: Graph[Boolean, Double] = graph.mapVertices((id, _) => id == sourceId)
val sssp = initialGraph.pregel(false)(
(id, canReach, newCanReach) => canReach || newCanReach, // Vertex Program
triplet => { // Send Message
if (triplet.srcAttr && !triplet.dstAttr) {
Iterator((triplet.dstId, true))
} else {
Iterator.empty
}
},
(a, b) => a || b // Merge Message
)
println(sssp.vertices.collect.mkString("\n"))
【讨论】: