graphframes是一个基于 PySpark DataFrames 的网络分析工具。以下代码是教程子图示例的修改版本:
from graphframes.examples import Graphs
import graphframes
g = Graphs(sqlContext).friends() # Get example graph
# Select subgraph of users older than 30
v2 = g.vertices.filter("age > 30")
g2 = graphframes.GraphFrame(v2, g.edges)
与原始图 g
相比,新图 g2
将包含更少的节点和边。
然而,事实并非如此:
print(g.vertices.count(), g.edges.count())
print(g2.vertices.count(), g2.edges.count())
给出输出:
(6, 7)
(7, 4)
很明显,生成的图包含不存在节点的边。
更令人不安的是 g.degrees
和 g2.degrees
是相同的。这意味着至少一些图形功能会忽略节点信息。有没有一种好方法可以确保 GraphFrame
创建
仅使用提供的 nodes
和 edges
参数的交集的图形?
最佳答案
我用来对图框进行子图化的一种方法是使用图案:
motifs = g.find("(a)-[e]->(b)").filter(<conditions for a,b or e>)
new_vertices = sqlContext.createDataFrame(motifs.map(lambda row: row.a).union(motifs.map(lambda row: row.b)).distinct())
new_edges = sqlContext.createDataFrame(motifs.map(lambda row:row.e).distinct())
new_graph = GraphFrame(new_vertices,new_edges)
虽然这看起来更复杂并且在运行时可能需要更长的时间,但对于更复杂的图形查询,这非常适合作为单个实体而不是作为单独的顶点和边与图形框架进行交互。因此,对顶点的过滤也会影响留在图框中的边。
关于python - PySpark GraphFrame 的正确子图化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37726286/