gpt4 book ai didi

algorithm - 如何用graphx求和边缘权重

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:20:01 25 4
gpt4 key购买 nike

我有一个 Graph[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()

关于algorithm - 如何用graphx求和边缘权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35648558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com