我试图找到从 GraphFrames 函数 shortestPaths 获取 Map 输出并将每个顶点的距离映射展平为新 DataFrame 中的单独行的最有效方法。通过将距离列拉到字典中,然后从那里转换为熊猫数据框,然后再转换回 Spark 数据框,我已经能够非常笨拙地做到这一点,但我知道必须有更好的方法。
from graphframes import *
v = sqlContext.createDataFrame([
("a", "Alice", 34),
("b", "Bob", 36),
("c", "Charlie", 30),
], ["id", "name", "age"])
# Create an Edge DataFrame with "src" and "dst" columns
e = sqlContext.createDataFrame([
("a", "b", "friend"),
("b", "c", "follow"),
("c", "b", "follow"),
], ["src", "dst", "relationship"])
# Create a GraphFrame
g = GraphFrame(v, e)
results = g.shortestPaths(landmarks=["a", "b","c"])
results.select("id","distances").show()
+---+--------------------+
| id| distances|
+---+--------------------+
| a|Map(a -> 0, b -> ...|
| b| Map(b -> 0, c -> 1)|
| c| Map(c -> 0, b -> 1)|
+---+--------------------+
我想要的是获取上面的输出并拉平距离,同时将 id 保持为如下所示:
+---+---+---------+
| id| v | distance|
+---+---+---------+
| a| a | 0 |
| a| b | 1 |
| a| c | 2 |
| b| b | 0 |
| b| c | 1 |
| c| c | 0 |
| c| b | 1 |
+---+---+---------+
谢谢。