데이터를 가정하면 연결 구성 요소 cities = List("Houston","Omaha")
이제 도시 목록에있는 모든 도시를위한 도시 열에 필터를 실행 를 실행하고자하는 도시의 목록을 작성이
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")
처럼, 그런 다음 결과 데이터 프레임에서 에지 및 정점 데이터 프레임을 생성합니다. 연결 구성 요소 알고리즘을이 가장자리와 정점 dataframes에서 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|
+------+------------+
마십시오 두 번 같은 질문을 : https://stackoverflow.com/questions/46386182/how-would -i-phrase-this-python-code-in-pyspark-sql-or-sql – Mariusz
이전 버전을 삭제했습니다.이 버전은 더 나은 표현이 있습니다. – oliver