我通过我的EMR笔记本使用其他库时非常困难。 EMR的AWS界面允许我创建Jupyter笔记本并将它们附加到正在运行的集群。我想在其中使用其他库。通过SSH连接到机器并手动安装为ec2-user
或root
将不会使库可用于笔记本,因为它显然使用livy
用户。引导操作为hadoop
安装东西。我无法从笔记本电脑安装,因为它的用户显然没有sudo
,git
等,并且它可能无论如何都不会安装到奴隶。
为通过EMR界面创建的笔记本安装附加库的规范方法是什么?
为了举例,我们假设您在运行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]
在这种情况下,我通常做的是删除我的群集并创建一个带有引导操作的新群集。 Bootstrap操作允许您在群集上安装其他库: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界面创建集群时可以执行完全相同的操作,方法是转到创建的高级选项。
EMR Notebooks最近推出了“笔记本范围库”,您可以使用它从公共或私有PyPI存储库在集群上安装其他Python库,并在笔记本会话中使用它。
笔记本范围的库提供以下好处:
这里有更多细节,https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-notebooks-scoped-libraries.html