你应该看看 GraphFrames (https://github.com/graphframes/graphframes https://github.com/graphframes/graphframes),它将 GraphX 算法包装在 DataFrames API 下,并提供 Python 接口。
这是一个简单的例子https://graphframes.github.io/graphframes/docs/_site/quick-start.html https://graphframes.github.io/graphframes/docs/_site/quick-start.html,稍作修改即可正常工作
首先启动 pyspark 并加载 graphframes pkg
pyspark --packages graphframes:graphframes:0.1.0-spark1.6
蟒蛇代码:
from graphframes import *
# Create a Vertex DataFrame with unique ID column "id"
v = sqlContext.createDataFrame([
("a", "Alice", 34),
("b", "Bob", 36),
("c", "Charlie", 30),
], ["id", "name", "age"])
# Create an Edge DataFrame with "src" and "dst" columns
e = sqlContext.createDataFrame([
("a", "b", "friend"),
("b", "c", "follow"),
("c", "b", "follow"),
], ["src", "dst", "relationship"])
# Create a GraphFrame
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()