如何使用GraphX计算邻居的平均程度

时间:2016-11-23 22:47:48

标签: apache-spark spark-graphx

我想计算图表中每个节点的平均邻居度。假设我们有这样的图表:

 val users: RDD[(VertexId, String)] = 
         sc.parallelize(Array((3L, "rxin"), 
                              (7L, "jgonzal"),
                              (5L, "franklin"), 
                              (2L, "istoica")))
// Create an RDD for edges
val relationships: RDD[Edge[Int]] = sc.parallelize(
                    Array(Edge(3L, 7L, 12),
                          Edge(5L, 3L, 1),
                          Edge(2L, 5L, 3), 
                          Edge(5L, 7L, 5)))
// Build the initial Graph
val graph = Graph(users, relationships)

修改 要了解结果,请选择节点5及其邻居:

  • 具有度= 2
  • 的节点3
  • 节点7,其度数= 2
  • 具有度= 1
  • 的节点2

此度量的输出只是节点5的邻居的平均度:(2 + 2 + 1)/ 3 = 1.666

理想情况下,您希望在此计算中删除节点5的链接,但这对我来说并不重要......

结束编辑

我正在尝试应用aggregateMessages,但是当我进入aggregateMessages调用时,我不知道如何检索每个节点的程度:

val neideg = g.aggregateMessages[(Long, Double)](
    triplet => {
      val comparedAttrs = compareAttrs(triplet.dstAttr, triplet.srcAttr) // BUT HERE I SHOULD GIVE ALSO THE DEGREE
      triplet.sendToDst(1L, comparedAttrs)
      triplet.sendToSrc(1L, comparedAttrs)
    },
    { case ((cnt1, v1), (cnt2, v2)) => (cnt1 + cnt2, v1 + v2) })

val aveneideg = neideg.mapValues(kv => kv._2 / kv._1.toDouble).toDF("id", "aveneideg")

然后我有一个完成总和的函数:

def compareAttrs(xs: (Int, String), ys: (Int, String)): Double = {
    xs._1.toDouble + ys._1.toDouble
}

如何传递到compareAttrs也是那些节点的度数值?

当然,与我正在尝试制作的解决方案相比,非常高兴能够看到更简单,更智能的解决方案......

1 个答案:

答案 0 :(得分:1)

我不清楚你之后是否会这样,但这是你可以选择的:

val degrees = graph.degrees
// now we have a graph where attribute is a degree of a vertex
val graphWithDegrees = graph.outerJoinVertices(degrees) { (_, _, optDegree) =>
    optDegree.getOrElse(1)    
}

// now each vertex sends its degree to its neighbours
// we aggregate them in a set where each vertex gets all values
// of its neighbours
val neighboursDegreeAndCount = graphWithDegrees.aggregateMessages[List[Long]](
    sendMsg = triplet => {
        val srcDegree = triplet.srcAttr
        val dstDegree = triplet.dstAttr
        triplet.sendToDst(List(srcDegree))
        triplet.sendToSrc(List(dstDegree))
    },
    mergeMsg = (x, y) => x ++ y
).mapValues(degrees => degrees.sum / degrees.size.toDouble)

// now if you want it in the original graph you can do
// outerJoinVertices again, and now the attr of vertex 
// in the graph is avg of its neighbours
graph.outerJoinVertices(neighboursDegreeAndCount) { (_, _, optAvgDegree) =>
    optAvgDegree.getOrElse(1)
}

因此,对于您的示例,输出为:Array((5,1.6666666666666667), (2,3.0), (3,2.5), (7,2.5))

相关问题