spark graphx 连通图子图顶点数量计算
spark graphx构建的图各个连同子图定数量计算
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder.appName("Simple Application").getOrCreate()
val sc = spark.sparkContext
// Create an RDD for the vertices
val users: RDD[(VertexId, (String, String))] =
sc.parallelize(Seq(
(3L, ("rxin", "student")),
(7L, ("jgonzal", "postdoc")),
(5L, ("franklin", "prof")),
(2L, ("istoica", "prof")),
(1L, ("seagle", "dev")),
(8L, ("zhangsan", "dev")),
(4L, ("peter", "student"))))
// Create an RDD for edges
val relationships: RDD[Edge[String]] =
sc.parallelize(Seq(
Edge(3L, 7L, "collab"),
Edge(5L, 3L, "advisor"),
Edge(2L, 5L, "colleague"),
Edge(5L, 7L, "pi"),
Edge(1L, 8L, "colleague"),
Edge(4L, 0L, "student"),
Edge(5L, 0L, "colleague")))
// 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)
val vertices : VertexRDD[VertexId] = graph.connectedComponents(3).vertices
vertices.collect().groupBy(_._2).foreach({g => println("连通图 id " ,g._1," 顶点数量 ",g._2.length)})
}