4

我正在使用 Scala 中的 Graph 框架应用 BFS,如何对所选最短路径的边权重求和。我有以下代码:

    import org.graphframes._
    import org.apache.spark.sql.DataFrame
    val v = sqlContext.createDataFrame(List(
      ("1", "Al"),
      ("2", "B"),
      ("3", "C"),
      ("4", "D"),
      ("5", "E")
    )).toDF("id", "name")

    val e = sqlContext.createDataFrame(List(
      ("1", "3", 5),
      ("1", "2", 8),
      ("2", "3", 6),
      ("2", "4", 7),
      ("2", "1", 8),
      ("3", "1", 5),
      ("3", "2", 6),
      ("4", "2", 7),
      ("4", "5", 8),
      ("5", "4", 8)
    )).toDF("src", "dst", "property")
val g = GraphFrame(v, e)
val paths: DataFrame = g.bfs.fromExpr("id = '1'").toExpr("id = '5'").run()
paths.show()

以上代码的输出为:

+------+-------+-----+-------+-----+-------+-----+                              
|  from|     e0|   v1|     e1|   v2|     e2|   to|
+------+-------+-----+-------+-----+-------+-----+
|[1,Al]|[1,2,8]|[2,B]|[2,4,7]|[4,D]|[4,5,8]|[5,E]|
+------+-------+-----+-------+-----+-------+-----+

但我需要这样的输出:

+----+-------+-----------+---------+
|    |source |Destination| Distance|
+----+-------+-----------+---------+
| e0 |   1    | 2        | 8       |
+----+-------+-----------+---------+
| e1 |   2    | 4        | 7       |
+----+-------+-----------+---------+
| e2 |   4    | 5        | 8       |
+----+-------+-----------+---------+

与上面的示例不同,我的图很大,它实际上可能会返回大量边。

4

0 回答 0