如何 display/visualize 由 GraphFrame 创建的图形?

How to display/visualize a graph created by GraphFrame?

我使用 GraphFrame 创建了一个图表

g = GraphFrame (vertices, edges)

除了使用 GraphFrame 提供的查询和属性分析图形外,我还想可视化图形以在演示文稿中使用。

您是否知道任何 tool/library / API / 允许以简单方式进行可视化的代码?

不是简单的方法,但是你可以使用 python-igraph 库,https://igraph.org/。我从 R 使用它,但 python 应该是相似的。请参阅下面的简单示例。主要问题用的都是那个工具,大家要仔细select小子图来画。

安装它:

#>pip install python-igraph

最简单的可视化:

g = GraphFrame (vertices, edges)
from igraph import *
ig = Graph.TupleList(g.edges.collect(), directed=True)
plot(ig)

另一种方法是使用图形模块 networkx 中的绘图功能

import networkx as nx
from graphframes import GraphFrame

def PlotGraph(edge_list):
    Gplot=nx.Graph()
    for row in edge_list.select('src','dst').take(1000):
        Gplot.add_edge(row['src'],row['dst'])

    plt.subplot(121)
    nx.draw(Gplot)


spark = SparkSession \
    .builder \
    .appName("PlotAPp") \
    .getOrCreate()

sqlContext = SQLContext(spark)

vertices = sqlContext.createDataFrame([
  ("a", "Alice", 34),
  ("b", "Bob", 36),
  ("c", "Charlie", 30),
  ("d", "David", 29),
  ("e", "Esther", 32),
("e1", "Esther2", 32),
  ("f", "Fanny", 36),
  ("g", "Gabby", 60),
    ("h", "Mark", 61),
    ("i", "Gunter", 62),
    ("j", "Marit", 63)], ["id", "name", "age"])

edges = sqlContext.createDataFrame([
  ("a", "b", "friend"),
  ("b", "a", "follow"),
  ("c", "a", "follow"),
  ("c", "f", "follow"),
  ("g", "h", "follow"),
  ("h", "i", "friend"),
  ("h", "j", "friend"),
  ("j", "h", "friend"),
    ("e", "e1", "friend")
], ["src", "dst", "relationship"])

g = GraphFrame(vertices, edges)
PlotGraph(g.edges)

另见 PYSPARK: how to visualize a GraphFrame?