5

我正在尝试在 AWS EMR 上的 Jupyter Notebook 中使用 pyspark 中的graphframes包(使用 Sagemaker 和 sparkmagic)。在 AWS 控制台中创建 EMR 集群时,我尝试添加配置选项:

[{"classification":"spark-defaults", "properties":{"spark.jars.packages":"graphframes:graphframes:0.7.0-spark2.4-s_2.11"}, "configurations":[]}]

但是当我尝试在 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()

这是输出/错误:

ImportError: No module named graphframes

我通读了这个 git 线程,但所有潜在的解决方法似乎都非常复杂,需要通过 ssh 连接到 EMR 集群的主节点。

4

2 回答 2

7

我终于发现有一个用于 graphframes 的 PyPi 包。我用它来创建一个详细的引导动作here,虽然我做了一些改变。

这是我为使图形框架在 EMR 上工作所做的工作:

  1. 首先,我创建了一个 shell 脚本并将其保存为名为“install_jupyter_libraries_emr.sh”的 s3:
#!/bin/bash

sudo pip install graphframes
  1. 然后,我在 AWS 控制台中完成了高级选项 EMR 创建过程。
    • 在第 1 步中,我在编辑软件设置文本框中添加了 graphframes 包的 maven 坐标:
    [{"classification":"spark-defaults","properties":{"spark.jars.packages":"graphframes:graphframes:0.7.0-spark2.4-s_2.11"}}]
    
    • 在第 3 步:常规集群设置中,我进入了引导操作部分
    • 在引导操作部分,我添加了一个新的自定义引导操作:
      • 任意名称
      • 我的“install_jupyter_libraries_emr.sh”脚本的 s3 位置
      • 没有可选参数
    • 然后我开始创建集群
  2. 集群启动后,我进入 Jupyter 并运行我的代码:
# 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()

这一次,终于,我得到了正确的输出:

+---+--------+
| id|inDegree|
+---+--------+
|  c|       1|
|  b|       2|
+---+--------+

+---+------------------+
| id|          pagerank|
+---+------------------+
|  b|1.0905890109440908|
|  a|              0.01|
|  c|1.8994109890559092|
+---+------------------+
于 2019-06-04T14:47:52.257 回答
7

@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/"
  }
 }
]
于 2021-06-17T16:40:10.260 回答