Bob*_*ain 5 amazon-emr apache-spark pyspark jupyter-notebook graphframes
我正在尝试在 AWS EMR 上的 Jupyter Notebook(使用 Sagemaker 和 sparkmagic)中使用 pyspark 中的graphframes包。在 AWS 控制台中创建 EMR 集群时,我尝试添加一个配置选项:
[{"classification":"spark-defaults", "properties":{"spark.jars.packages":"graphframes:graphframes:0.7.0-spark2.4-s_2.11"}, "configurations":[]}]
Run Code Online (Sandbox Code Playgroud)
但是在 jupyter notebook 中尝试在我的 pyspark 代码中使用 graphframes 包时,我仍然遇到错误。
这是我的代码(来自graphframes示例):
# 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()
Run Code Online (Sandbox Code Playgroud)
这是输出/错误:
ImportError: No module named graphframes
Run Code Online (Sandbox Code Playgroud)
我通读了这个 git 线程,但所有潜在的解决方法似乎都非常复杂,需要通过 ssh 连接到 EMR 集群的主节点。
我终于发现有一个用于 graphframes的PyPi 包。我用它来创建一个引导动作,详细说明在这里,虽然我改变了一些东西。
这是我为使图形框架在 EMR 上工作所做的工作:
#!/bin/bash
sudo pip install graphframes
Run Code Online (Sandbox Code Playgroud)
[{"classification":"spark-defaults","properties":{"spark.jars.packages":"graphframes:graphframes:0.7.0-spark2.4-s_2.11"}}]
Run Code Online (Sandbox Code Playgroud)
# 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()
Run Code Online (Sandbox Code Playgroud)
这一次,终于,我得到了正确的输出:
+---+--------+
| id|inDegree|
+---+--------+
| c| 1|
| b| 2|
+---+--------+
+---+------------------+
| id| pagerank|
+---+------------------+
| b|1.0905890109440908|
| a| 0.01|
| c|1.8994109890559092|
+---+------------------+
Run Code Online (Sandbox Code Playgroud)
@Bob Swain 的回答很好,但现在图形框架的存储库位于https://repos.spark-packages.org/。因此,为了使其工作,分类应更改为:
[
{
"classification":"spark-defaults",
"properties":{
"spark.jars.packages":"graphframes:graphframes:0.8.0-spark2.4-s_2.11",
"spark.jars.repositories":"https://repos.spark-packages.org/"
}
}
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1137 次 |
| 最近记录: |