如何用graphx求和边缘权重

how to sum edge weights with graphx

我有一个图 [Int, Int],其中每条边都有一个权重值。我想要做的是,对于每个用户,收集所有入边并计算与每个入边相关的权重。

说数据是这样的:

    import org.apache.spark.graphx._
    val sc: SparkContext
        // Create an RDD for the vertices
        val users: RDD[(VertexId, (String, String))] = 
             sc.parallelize(Array((3L, ("rxin", "student")), 
                                  (7L,("jgonzal", "postdoc")),
                                  (5L, ("franklin", "prof")), 
                                  (2L, ("istoica", "prof"))))
    // 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)))

    // Define a default user in case there are relationship with missing user
    val defaultUser = ("John Doe", "Missing")

    // Build the initial Graph
    val graph = Graph(users, relationships, defaultUser)

我的理想结果是一个带有顶点 ID 和总权重值的数据框...它基本上是一个加权入度度量...

id    value
3L    1
5L    3
7L    17
2L    0
val temp = graph.aggregateMessages[int](triplet => {triplet.sendToDst(triplet.attr)},_ + _, TripletFields.EdgeOnly).toDF("id","value")

temp.show()