Spark Graphx圖計算之二跳鄰算法實戰!
def sendMsgFunc(edge:EdgeTriplet[Int, Int]) = {
if(edge.srcAttr <= 0){
if(edge.dstAttr <= 0){
// 如果雙方都小於0,則不發送信息
Iterator.empty
}else{
// srcAttr小於0,dstAttr大於零,則將dstAttr-1後發送
Iterator((edge.srcId, edge.dstAttr - 1))
}
}else{
if(edge.dstAttr <= 0){
// srcAttr大於0,dstAttr<0,則將srcAttr-1後發送
Iterator((edge.dstId, edge.srcAttr - 1))
}else{
// 雙方都大於零,則將屬性-1後發送
val toSrc = Iterator((edge.srcId, edge.dstAttr - 1))
val toDst = Iterator((edge.dstId, edge.srcAttr - 1))
toDst ++ toSrc
}
}
}
val friends = Pregel(
graph.mapVertices((vid, value)=> if(vid == 1) 2 else -1),
// 發送初始值
-1,
// 指定階數
2,
// 雙方向發送
EdgeDirection.Either
)(
// 將值設爲大的一方
vprog = (vid, attr, msg) => math.max(attr, msg),
//
sendMsgFunc,
//
(a, b) => math.max(a, b)
).subgraph(vpred = (vid, v) => v >= 0)
println("\n\n~~~~~~~~~ Confirm Vertices of friends ")
friends.vertices.collect.foreach(println(_))
// (4,1)
// (8,0)
// (2,1)
// (1,2)
// (3,0)
// (5,0)
sc.stop