我在通过我的 EMR 笔记本处理其他库时遇到了令人惊讶的困难。 EMR 的 AWS 接口(interface)允许我创建 Jupyter 笔记本并将它们附加到正在运行的集群。我想在其中使用其他库。通过 SSH 连接到机器并以 ec2-user
或 root
手动安装不会使这些库可供笔记本使用,因为它显然使用了 livy
用户。引导操作为 hadoop
安装东西。我无法从笔记本安装,因为它的用户显然没有 sudo
、git
等,而且它可能无论如何都不会安装到 slaves。
为通过 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]
关于bash - EMR 笔记本安装额外的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54697280/