2016-06-09 45 views
2

graphframes是基于PySpark DataFrame的网络分析工具。以下代码是教程subgraphing例的修改后的版本:PySpark GraphFrame的正确子图

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) 

人们会期望新的曲线图中,g2将包含更少的节点和更少的边缘,相对于原来的,g。 然而,这不是这种情况:

print(g.vertices.count(), g.edges.count()) 
print(g2.vertices.count(), g2.edges.count()) 

给出的输出:

(6, 7) 
(7, 4) 

显而易见的是,所得到的曲线图中包含不存在的节点的边。 更令人不安的是g.degreesg2.degrees是相同的。这意味着至少有一些图形功能会忽略节点信息。有没有一种好方法可以确保GraphFrame只使用提供的nodesedges参数的交集创建 ?

回答

1

有趣的..我没能看到结果:

>>> 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) 
>>> print(g.vertices.count(), g.edges.count()) 
(6, 7) 
>>> print(g2.vertices.count(), g2.edges.count()) 
(4, 7) 

GraphFrames截至目前不检查图是有效的 - 即。在构建图的时候,所有的边都连接到顶点等等。但似乎过滤器后的顶点数是正确的?

+0

>但似乎过滤器后的顶点数是正确的? 它是,但不是边缘的数量。删除顶点应该也会导致删除一些边缘 –

1

,我用它来子图graphframe一种方法是使用主题:

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) 

虽然这看起来更加复杂,并可能在运行方面需要更长的时间,对于更复杂的图形查询,这可以很好地充当你互动将图框作为单个实体,而不是将顶点和边缘分开。因此,对顶点进行过滤也会影响图框中留下的边缘。