python - PySpark GraphFrame 的正确子图化

标签 python pyspark graphframes

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.degreesg2.degrees 是相同的。这意味着至少一些图形功能会忽略节点信息。有没有一种好方法可以确保 GraphFrame 创建 仅使用提供的 nodesedges 参数的交集的图形?

最佳答案

我用来对图框进行子图化的一种方法是使用图案:

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/

相关文章:

python - 在 PySpark 数据框中添加列总和作为新列

python - 如何在 flatMap 函数中实现迭代

apache-spark - GraphFrames 主题搜索的边缘属性过滤器不起作用

python - Elementtree,检查元素是否有特定的父元素?

python - 在 Python 中多久使用一次静态方法

python - python中的多线程是一个神话吗?

python - 在 spark 中计数和收集函数时抛出 IllegalArgumentException

pyspark - 如何使用 pyspark graphframe pregel API 实现循环检测

apache-spark - 如何仅从 Edge DataFrame 制作 GraphFrame

python - 为什么 `QtGui.QValidator.validate`的返回如此不一致?处理这个问题的可靠方法?