GraphX中Pregel API中顶点程序和Merge Message部分之间的区别

时间:2016-04-29 17:15:00

标签: spark-graphx

我是GraphX的新手,我不了解Pregel API中的顶点程序和合并消息部分。不要做同样的事情吗? 例如,从Spark网站获取的以下Pregel代码中Vertex Program和Merge Message部分之间的区别是什么?

import org.apache.spark.graphx._
// Import random graph generation library
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 distance infinity.
val initialGraph = graph.mapVertices((id, _) => if (id == sourceId) 0.0 else Double.PositiveInfinity)
val sssp = initialGraph.pregel(Double.PositiveInfinity)(
  (id, dist, newDist) => math.min(dist, newDist), **// Vertex Program**
  triplet => {  // Send Message
    if (triplet.srcAttr + triplet.attr < triplet.dstAttr) {
      Iterator((triplet.dstId, triplet.srcAttr + triplet.attr))
    } else {
      Iterator.empty
    }
  },
  (a,b) => math.min(a,b) **// Merge Message**
  )
println(sssp.vertices.collect.mkString("\n"))

1 个答案:

答案 0 :(得分:2)

首先,mergeMsg部分无法访问任何Vertex的上下文 - 它只需要单独的消息并创建单个消息。该消息反过来作为单个消息发送到vprog

因此,vprog无法访问单个邮件,只能访问 total (无论这意味着什么)。并且mergeMsg只能接收两条消息并创建一条消息。 mergeMessage发生,直到只剩下一条消息 - 总计 - 正如我所说的那样传递给vprog