如何在 Amazon EMR 集群中永久安装 Spark 或基于 Scala 的库?

上次更新时间:2020 年 11 月 20 日

如何在 Amazon EMR 集群上永久安装软件包,然后在 EMR 笔记本上访问该软件包?

解决方法

以下示例从 GraphFrames 网站安装 GraphFrames。请按照以下步骤永久安装要在 EMR 笔记本的 PySpark 内核上访问的任何基于 Spark 或 Scala 的库。

准备引导操作脚本

1. 为您的库下载 JAR。

2. 将 JAR 上传到 Amazon Simple Storage Service (Amazon S3) 存储桶。

3.创建类似以下内容的引导操作脚本。此示例脚本会在 Amazon EMR 集群的所有节点上自动安装 GraphFrame 库。将 s3://doc-example-bucket/graphframes-0.8.0-spark2.4-s_2.11.jar 替换为 S3 存储桶中 JAR 的路径。

#!/bin/bash
# These two following statements install the graphframes library on all nodes of an EMR cluster for Python base version 2.7 and Python 3.
sudo pip-3.6 install graphframes
sudo pip install graphframes
# The following statement copies the GraphFrames Spark jar from an S3 bucket to all nodes of an EMR cluster on the required path.
sudo aws s3 cp s3://doc-example-bucket/graphframes-0.8.0-spark2.4-s_2.11.jar /usr/lib/spark/jars/

3. 将引导操作脚本上传到 S3 存储桶。

4. 打开 Amazon EMR 控制台。

5. 选择 Create cluster(创建集群),然后选择 Go to advanced options(转到高级选项)。

6. 在 Software configuration(软件配置)部分,选择 Hive、Livy 和 Spark。运行 EMR notebooks 需要这些软件包。有关更多信息,请参阅集群要求。

7. 继续创建集群。在步骤 3:常规集群设置页面上,输入引导操作脚本的路径。有关更多信息,请参阅使用控制台添加自定义引导操作。

8. 完成集群的创建。

9.使用刚创建的集群创建 EMR 笔记本。

10. 笔记本就绪后,选择在 JupyterLab 中打开

11. 要使用 PySpark 内核测试 PySpark 代码,请运行类似于以下内容的代码片段。如果此代码成功,GraphFrames 即正确安装。

          from pyspark import *
          from pyspark.sql import *
          from graphframes import *
          spark = SparkSession.builder.appName('fun').getOrCreate()
          vertices = spark.createDataFrame(,
                                           )
          edges = spark.createDataFrame(,
                                        )
          g = GraphFrame(vertices, edges)
          ## Take a look at the DataFrames
          g.vertices.show()
          g.edges.show()
          ## Check the number of edges of each vertex
          g.degrees.show()