以mapReduceTriplets
代码为例,sendMsg中id决定了接收消息的点,而activeSetOpt: Option[(VertexRDD[_], EdgeDirection)] = None
中的EdgeDirection指定了哪些和顶点相邻的边处理sendMsg。如果该方向是in,则作用于目标顶点在VertexRDD中的边。如果方向是out,则作用在那些源顶点在vertexRDD中的边。如果方向是 either,则 map 函数将仅在任一顶点在
vertexRDD
中的边。如果方向是 both,则map函数将仅作用在两个顶点都在活跃集vertexRDD中。活
跃集合必须来自图的顶点中。
即activeSetOpt决定了作用边,sendMsg中的id决定了接收消息的点是目的顶点还是源顶点
private[graphx] def mapReduceTriplets[VD: ClassTag, ED: ClassTag, A: ClassTag](
g: Graph[VD, ED],
mapFunc: EdgeTriplet[VD, ED] => Iterator[(VertexId, A)],
reduceFunc: (A, A) => A,
activeSetOpt: Option[(VertexRDD[_], EdgeDirection)] = None): VertexRDD[A] = {
def sendMsg(ctx: EdgeContext[VD, ED, A]) {
mapFunc(ctx.toEdgeTriplet).foreach { kv =>
val id = kv._1
val msg = kv._2
if (id == ctx.srcId) {
ctx.sendToSrc(msg)
} else {
assert(id == ctx.dstId)
ctx.sendToDst(msg)
}
}
}
g.aggregateMessagesWithActiveSet(
sendMsg, reduceFunc, TripletFields.All, activeSetOpt)
}