Graph pyspark中连通分量的高效计算
我正在尝试为城市中的朋友查找连接的组件。我的数据是具有城市属性的边列表 城市| SRC | DEST 休斯顿凯尔->本尼 休斯顿本尼->查尔斯 休斯顿查尔斯->丹尼 奥马哈卡罗尔->布莱恩 等等 我知道pyspark的GraphX库的connectedComponents函数将遍历图的所有边以查找连接的组件,我希望避免这种情况。我该怎么做 编辑: 我想我可以做一些类似的事情 从数据框中选择已连接的_组件(*) groupby城市Graph pyspark中连通分量的高效计算,graph,spark-dataframe,spark-graphx,connected-components,graphframes,Graph,Spark Dataframe,Spark Graphx,Connected Components,Graphframes,我正在尝试为城市中的朋友查找连接的组件。我的数据是具有城市属性的边列表 城市| SRC | DEST 休斯顿凯尔->本尼 休斯顿本尼->查尔斯 休斯顿查尔斯->丹尼 奥马哈卡罗尔->布莱恩 等等 我知道pyspark的GraphX库的connectedComponents函数将遍历图的所有边以查找连接的组件,我希望避免这种情况。我该怎么做 编辑: 我想我可以做一些类似的事情 从数据框中选择已连接的_组件(*) groupby城市 连接的组件将生成项目列表。假设您的数据如下 import org.
连接的组件将生成项目列表。假设您的数据如下
import org.apache.spark._
import org.graphframes._
val l = List(("Houston","Kyle","Benny"),("Houston","Benny","charles"),
("Houston","Charles","Denny"),("Omaha","carol","Brian"),
("Omaha","Brian","Daniel"),("Omaha","Sara","Marry"))
var df = spark.createDataFrame(l).toDF("city","src","dst")
创建要为其运行连接组件的城市列表
cities=List(“休斯顿”、“奥马哈”)
现在为“城市中的每个城市”列表中的“城市”列运行过滤器,然后从生成的数据帧创建边和顶点数据帧。从这些边和顶点数据帧创建一个graphframe,并运行连接组件算法
val cities = List("Houston","Omaha")
for(city <- cities){
val edges = df.filter(df("city") === city).drop("city")
val vert = edges.select("src").union(edges.select("dst")).
distinct.select(col("src").alias("id"))
val g = GraphFrame(vert,edges)
val res = g.connectedComponents.run()
res.select("id", "component").orderBy("component").show()
}
避免问同一个问题两次:删除旧的一个,这一个有更好的措辞。谢谢你的工作!好吧,该死的。我想可能有一些东西更接近金属,而不仅仅是循环通过我想要阻止的值,但我仍然感谢你的回答
| id| component|
+-------+------------+
| Kyle|249108103168|
|charles|249108103168|
| Benny|249108103168|
|Charles|721554505728|
| Denny|721554505728|
+-------+------------+
+------+------------+
| id| component|
+------+------------+
| Marry|858993459200|
| Sara|858993459200|
| Brian|944892805120|
| carol|944892805120|
|Daniel|944892805120|
+------+------------+