如何在AWS EMR上的pyspark中使用图框?

时间:2019-06-04 14:47:52

标签: apache-spark pyspark jupyter-notebook amazon-emr graphframes

我正在尝试在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笔记本的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

我通读了this git thread,但所有可能的解决方法似乎都非常复杂,需要将其切入EMR群集的主节点中。

2 个答案:

答案 0 :(得分:4)

上面的答案很好,但现在图形框架的存储库位于 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/"
  }
 }
]

答案 1 :(得分:2)

我终于发现有一个PyPi package for graphframes。我用它来创建了一个详细的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:“常规群集设置”中,我进入了“引导操作”部分
    • 在引导操作部分中,我添加了一个新的自定义boostrap操作,其内容如下:
      • 任意名称
      • 我的“ 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|
+---+------------------+