【问题标题】:How to get a list of the connected components of a graph using GraphX's Java APIs如何使用 GraphX 的 Java API 获取图的连接组件列表
【发布时间】:2021-04-29 19:20:12
【问题描述】:

我对 spark 和 GraphX 还很陌生,我正在尝试了解如何使用 GraphX 的 Java API 执行以下操作。我正在寻找具有以下签名的方法:

private <List<Graph<VD, ED>> computeConnectedComponents(Graph<VD, ED> graph){}

如果给定一个只有正度节点的图,但连接组件的数量未知,它应该返回一个图列表(顺序无关紧要),其中每个图都是连接的。

我知道GraphOps.connectedComponents()ConnectedComponents.run(),但我很难理解返回值。文档将它们列为返回 Graph&lt;Object, ED&gt; 的图表,并说明返回的“最低顶点 id”。

基本上,我想知道如何从 connectedComponents 的返回值和我的初始图导出这个图列表。

【问题讨论】:

    标签: java apache-spark spark-graphx


    【解决方案1】:

    以下代码在 scala 中,但应该演示这个想法。

    返回的图会包含所有的顶点,但是每个顶点的属性都被替换为一个VertexId(实际上只是一个Long),可以解释为该顶点所属的连通组件的id。它也是属于该连接组件的“最低顶点 id”。

    import org.apache.spark.graphx._
    import org.apache.spark.rdd.RDD
    val vertexArray = Array(
      (1L, ("A", 28)),
      (2L, ("B", 27)),
      (3L, ("C", 65)),
      (4L, ("D", 42)),
      (5L, ("E", 55)),
      (6L, ("F", 50)),
      (7L, ("G", 53)),
      (8L, ("H", 66))
      )
    
    // Vertices 1 - 6 are connected, 7 and 8 are connected.
    val edgeArray = Array(
      Edge(2L, 1L, 7),
      Edge(2L, 4L, 2),
      Edge(3L, 2L, 4),
      Edge(3L, 6L, 3),
      Edge(4L, 1L, 1),
      Edge(5L, 2L, 2),
      Edge(5L, 3L, 8),
      Edge(5L, 6L, 3),
      Edge(7L, 8L, 3)
      )
    
    val vertexRDD: RDD[(Long, (String, Int))] = sc.parallelize(vertexArray)
    val edgeRDD: RDD[Edge[Int]] = sc.parallelize(edgeArray)
    val graph: Graph[(String, Int), Int] = Graph(vertexRDD, edgeRDD)
    
    val cc = graph.connectedComponents().vertices.collectAsMap()
    cc.foreach {
      case (vertexId, clusterId) =>
        println(s"Vertex $vertexId belongs to cluster $clusterId")
    }
    
    

    输出:

    Vertex 8 belongs to cluster 7
    Vertex 2 belongs to cluster 1
    Vertex 5 belongs to cluster 1
    Vertex 4 belongs to cluster 1
    Vertex 7 belongs to cluster 7
    Vertex 1 belongs to cluster 1
    Vertex 3 belongs to cluster 1
    Vertex 6 belongs to cluster 1
    

    【讨论】:

    • 谢谢,这很有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 2015-05-18
    相关资源
    最近更新 更多