您需要根據頂點和邊的聯合創建一個新的圖形,然後用groupEdges():
import org.apache.spark.graphx._
import org.apache.spark.graphx.PartitionStrategy.RandomVertexCut
val verts1 = sc.parallelize(Seq(
(1L,"A"),
(2L,"B"),
(3L,"C"),
(4L,"D"),
(5L,"E"),
(6L,"F")))
val edges1 = sc.parallelize(Seq(
Edge(1L,2L,1),
Edge(2L,3L,1),
Edge(3L,4L,1),
Edge(1L,5L,1),
Edge(5L,6L,1)))
val graph1 = Graph(verts1, edges1)
val verts2 = sc.parallelize(Seq(
(1L,"A"),
(2L,"B"),
(3L,"C"),
(7L,"G")))
val edges2 = sc.parallelize(Seq(
Edge(1L,2L,1),
Edge(2L,3L,1),
Edge(1L,7L,1)))
val graph2 = Graph(verts2, edges2)
val graph: Graph[String,Int] = Graph(
graph1.vertices.union(graph2.vertices),
graph1.edges.union(graph2.edges)
).partitionBy(RandomVertexCut).
groupEdges((attr1, attr2) => attr1 + attr2)
如果你現在看看這個新圖的邊你可以看到合併結果:
scala> graph.edges.collect
res0: Array[org.apache.spark.graphx.Edge[Int]] =
Array(Edge(1,2,2), Edge(2,3,2), Edge(1,5,1),
Edge(5,6,1), Edge(1,7,1), Edge(3,4,1))
嗨,謝謝你的回答。是否有可能根據頂點屬性的值而不是頂點ID進行聯合?因爲有時很難讓相同的頂點ID具有完全相同的屬性,反之亦然 – chihhsiw
順便說一句,這個答案有助於解決我的問題的一部分。但我有一個更開放的問題張貼在http://stackoverflow.com/questions/33221099/apache-graphx-merge-combine-multiple-graphs-according-to-vertex-property-of-rela。感謝你的幫助 – chihhsiw