【问题标题】:EMR notebooks install additional librariesEMR 笔记本安装额外的库
【发布时间】:2019-07-08 21:15:36
【问题描述】:

我在通过 EMR 笔记本使用其他库时遇到了令人惊讶的困难。 EMR 的 AWS 接口允许我创建 Jupyter 笔记本并将它们附加到正在运行的集群。我想在其中使用其他库。通过 SSH 连接到机器并以 ec2-userroot 的身份手动安装不会使这些库对笔记本可用,因为它显然使用了 livy 用户。引导操作为hadoop 安装东西。我无法从笔记本安装,因为它的用户显然没有sudogit 等,而且它可能无论如何也不会安装到从站。

为通过 EMR 界面创建的笔记本安装其他库的规范方法是什么?

【问题讨论】:

  • 如果我们的答案是否适合您,我们会很高兴获得一些反馈。
  • 这个问题格式错误,但我无法删除它。这里的问题是我是从源代码安装的,还涉及很多其他问题。对此感到抱歉。

标签: bash amazon-web-services jupyter-notebook libraries amazon-emr


【解决方案1】:

为了举例,假设您在running EMR 集群上需要librosa Python 模块。我们将使用 Python 2.7,因为过程更简单 - Python 2.7 保证在集群上,这是 EMR 的默认运行时。

创建一个安装包的脚本:

#!/bin/bash
sudo easy_install-2.7 pip
sudo /usr/local/bin/pip2 install librosa

并将其保存到您的主目录,例如/home/hadoop/install_librosa.sh。记下名字,我们稍后会用到。

在下一步中,您将通过另一个受Amazon EMR docs 启发的脚本运行此脚本:emr_install.py。它使用 AWS Systems Manager 在节点上执行您的脚本。

import time
from boto3 import client
from sys import argv

try:
  clusterId=argv[1]
except:
  print("Syntax: emr_install.py [ClusterId]")
  import sys
  sys.exit(1)

emrclient=client('emr')

# Get list of core nodes
instances=emrclient.list_instances(ClusterId=clusterId,InstanceGroupTypes=['CORE'])['Instances']
instance_list=[x['Ec2InstanceId'] for x in instances]

# Attach tag to core nodes
ec2client=client('ec2')
ec2client.create_tags(Resources=instance_list,Tags=[{"Key":"environment","Value":"coreNodeLibs"}])

ssmclient=client('ssm')

    # Run shell script to install libraries

command=ssmclient.send_command(Targets=[{"Key": "tag:environment", "Values":["coreNodeLibs"]}],
                               DocumentName='AWS-RunShellScript',
                               Parameters={"commands":["bash /home/hadoop/install_librosa.sh"]},
                               TimeoutSeconds=3600)['Command']['CommandId']

command_status=ssmclient.list_commands(
  CommandId=command,
  Filters=[
      {
          'key': 'Status',
          'value': 'SUCCESS'
      },
  ]
)['Commands'][0]['Status']

time.sleep(30)

print("Command:" + command + ": " + command_status)

运行它:

python emr_install.py [cluster_id]

【讨论】:

    【解决方案2】:

    在这种情况下,我通常会删除我的集群并使用引导操作创建一个新集群。引导操作允许您在集群上安装其他库:https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-plan-bootstrap.html。 例如,编写以下脚本并将其保存在 S3 中将允许您使用运行在集群顶部的笔记本中的 datadog(至少它适用于 EMR 5.19):

    #!/bin/bash -xe
    #install datadog module for using in pyspark
    sudo pip-3.4 install -U datadog
    

    这是我为启动此集群运行的命令行:

    aws emr create-cluster --release-label emr-5.19.0 \
    --name 'EMR 5.19 test' \
    --applications Name=Hadoop Name=Spark Name=Hive Name=Livy \
    --use-default-roles \
    --instance-groups \
    InstanceGroupType=MASTER,InstanceCount=1,InstanceType=m4.large \
    InstanceGroupType=CORE,InstanceCount=2,InstanceType=m4.large \
    --region eu-west-1 \
    --log-uri s3://<path-to-logs> \
    --configurations file://config-emr.json \
    --bootstrap-actions Path=s3://<path-to-bootstrap-in-aws>,Name=InstallPythonModules
    

    以及本地存储在您计算机上的 config-emr.json:

    [{
        "Classification": "spark",
        "Properties": {
        "maximizeResourceAllocation": "true"
        }
    },
    {
        "Classification": "spark-env",
        "Configurations": [
        {
            "Classification": "export",
            "Properties": {
                "PYSPARK_PYTHON": "/usr/bin/python3"
            }
        }
        ]
    }]   
    

    我假设您在通过 EMR 界面创建集群时可以执行完全相同的操作,方法是转到创建的高级选项。

    【讨论】:

    • 那么,这个库可用于 jupyter notebooks 吗?
    【解决方案3】:

    为通过 EMR 界面创建的笔记本安装其他库的规范方法是什么?

    EMR Notebooks 最近推出了“笔记本范围的库”,您可以使用它从公共或私有 PyPI 存储库在集群上安装其他 Python 库,并在笔记本会话中使用它。

    笔记本范围的库提供以下好处:

    • 您可以在 EMR 笔记本中使用库,而无需重新创建 集群或将笔记本重新附加到集群。
    • 您可以将 EMR 笔记本的库依赖项隔离到单个笔记本会话。从笔记本中安装的库不会干扰集群上的其他库或安装在其他笔记本会话中的库。

    这里有更多细节, https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-notebooks-scoped-libraries.html

    技术博客: https://aws.amazon.com/blogs/big-data/install-python-libraries-on-a-running-cluster-with-emr-notebooks/

    【讨论】:

      【解决方案4】:

      我在这方面花了很长时间,AWS 文档或支持根本没有帮助,但确实让它工作,以便您可以直接在笔记本中安装 python 库。

      如果您可以执行以下项目,那么您可以通过在单行 Jupyter 单元中运行 pip install 命令来安装库,使用 Python 运行时,就像这样

      !pip install pandas
      

      让我很困惑的一个问题是,我可以通过 SSH 进入集群并连接到互联网,ping 和 pip 都可以,但是笔记本无法连接,也没有任何库实际可用。相反,您需要确保笔记本可以伸出。一个很好的测试就是看看你是否可以ping通。结构同上,单行以!开始

      !ping google.com
      

      如果这花费的时间太长并且超时,那么您仍然需要弄清楚您的 VPN/子网规则。

      以下关于集群创建的注意事项:

      • (步骤 1)这不适用于每个版本的 EMR。我在 5.30.0 上运行它,但最后我检查了 5.30.1 没有运行。
      • (第 2 步 -> 联网)您需要确保您在私有子网上,并且您的 VPN 可以访问公共互联网。同样,不要让 SHHing 进入服务器欺骗你,笔记本要么在 docker 映像中,要么在其他地方运行。唯一相关的测试是您直接从笔记本运行的测试。

      一旦你有这个工作并安装一个包,它将适用于该集群上的任何笔记本。我有一个名为 install 的笔记本,每当我启动一个新集群时,每个包都会运行一行。

      【讨论】:

        猜你喜欢
        • 2019-10-24
        • 2020-06-28
        • 2020-04-09
        • 2022-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-19
        相关资源
        最近更新 更多