根据http://ampcamp.berkeley.edu/big-data-mini-course/graph-analytics-with-graphx.html提供的文档:
“GraphX API 目前仅在 Scala 中可用,但我们计划在未来提供 Java 和 Python 绑定。”
不过,你应该看看 GraphFrames (https://github.com/graphframes/graphframes),它在 DataFrames API 下封装了 GraphX 算法,并提供 Python 接口。
这是来自https://graphframes.github.io/graphframes/docs/_site/quick-start.html 的一个简单示例,稍作修改以使其正常工作。
首先,启动 pyspark 并加载图形框架 pkg。
pyspark --packages graphframes:graphframes:0.1.0-spark1.6
python 代码:
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()