快速入门
本快速入门指南展示了如何开始使用 GraphFrames。完成本指南后,请继续阅读用户指南以了解更多关于 GraphFrames 支持的众多查询和算法。
以下示例展示了如何创建一个GraphFrame、查询它以及运行PageRank算法。
Python 应用程序接口
# Create a Vertex DataFrame with unique ID column "id"
v = spark.createDataFrame([
("a", "Alice", 34),
("b", "Bob", 36),
("c", "Charlie", 30),
], ["id", "name", "age"])
# Create an Edge DataFrame with "src" and "dst" columns
e = spark.createDataFrame([
("a", "b", "friend"),
("b", "c", "follow"),
("c", "b", "follow"),
], ["src", "dst", "relationship"])
# Create a GraphFrame
from graphframes import *
g = GraphFrame(v, e)
# Query: Get in-degree of each vertex.
g.inDegrees.show()
# Query: Count the number of "follow" connections in the graph.
g.edges.filter("relationship = 'follow'").count()
# Run PageRank algorithm, and show results.
results = g.pageRank(resetProbability=0.01, maxIter=20)
results.vertices.select("id", "pagerank").show()
Scala API
// import graphframes package
import org.graphframes._
// Create a Vertex DataFrame with unique ID column "id"
val v = spark.createDataFrame(List(
("a", "Alice", 34),
("b", "Bob", 36),
("c", "Charlie", 30)
)).toDF("id", "name", "age")
// Create an Edge DataFrame with "src" and "dst" columns
val e = spark.createDataFrame(List(
("a", "b", "friend"),
("b", "c", "follow"),
("c", "b", "follow")
)).toDF("src", "dst", "relationship")
// Create a GraphFrame
import org.graphframes.GraphFrame
val g = GraphFrame(v, e)
// Query: Get in-degree of each vertex.
g.inDegrees.show()
// Query: Count the number of "follow" connections in the graph.
g.edges.filter("relationship = 'follow'").count()
// Run PageRank algorithm, and show results.
val results = g.pageRank.resetProbability(0.01).maxIter(20).run()
results.vertices.select("id", "pagerank").show()
图算法
历史上,Apache Spark 曾内置一个名为 GraphX 的图处理工具,该工具基于 RDD(Spark 2.x 之前的处理方式)。GraphX 提供了一系列图算法,如 PageRank、LabelPropagation 等。在 Spark 4.0.x 中,GraphX 已被弃用,不推荐使用。相反,GraphFrames 使用 Spark 的 Dataset / Dataframe 来表示图。GraphFrames 也提供了一系列标准图算法,并且这一集合正在不断增长。对于在 GraphX 中实现但目前在 GraphFrames 中尚未原生支持的算法,该库还提供了一个转换方法(参见 用户指南)。下表展示了当前支持的算法:
| 算法 | GraphX 包装器 | GraphFrames 实现 | 推荐 |
|---|---|---|---|
| 广度优先搜索 | 是 | 是 | GraphFrames 提供更流畅的 API |
| 连通组件 | 是 | 是 | 对于小型图和流式GraphX,否则使用GraphFrames |
| 强连通分量 | 是 | 否 | GraphX |
| 标签传播算法 | 是 | 是 | 对于小型图和流式GraphX,否则使用GraphFrames |
| PageRank | 是 | 否 | GraphX |
| 并行个性化网页排名 | 是 | 否 | GraphX |
| 最短路径 | 是 | 是 | 对于小型图和流式GraphX,否则使用GraphFrames |
| 三角形计数 | 是 | 是 | GraphFrames 提供更流畅的 API |
| SVD++ | 是 | 否 | GraphX |
| 循环检测 | 否 | 是 | GraphFrames |
| 三角形计数 | 否 | 是 | GraphFrames |
| K-核心 | 否 | 是 | GraphFrames |
| 最大独立集 | 否 | 是 | GraphFrames |