运行 Spark GraphX 中每个连接组件的 lambda

Run lambda per connected component in Spark GraphX

我正在尝试在 Spark 的 graphx 中为每个连接的组件执行一些 lambda。我使用 connectedComponents() 方法获取连接的组件,但后来我找不到任何其他方法,除了收集图形的所有不同顶点 ID 并将标签分配给组件,然后执行 foreach,并使用 subgraph() 方法获取每个组件。但这是顺序过程,如果我的图表有很多小组件,它就不可扩展。有人能帮我吗?有没有办法表达类似 connectedComponentsGraph.foreachComponent(lambda)?

我建议使用 graphframes:

 import org.graphframes._

 val graph: Graph = ???
 val gdf = GraphFrame.fromGraphX(graph)
 val components = gdf.connectedComponents.setAlgorithm("graphx").run()

并跟进基本 SQL:

components
  .join(gdf.vertices, Seq("id"))
  .join(gdf.edges.select($"src" as "id"), Seq("id"))
  .groupBy("component")
  .count