boto EMR添加步骤和自动终止

2024-06-12 00:28:51 发布

您现在位置:Python中文网/ 问答频道 /正文

Python2.7.12

boto3==1.3.1

如何将步骤添加到正在运行的EMR集群中,使该集群在步骤完成后终止,而不管其失败或成功与否?

创建群集

response = client.run_job_flow(
    Name=name,
    LogUri='s3://mybucket/emr/',
    ReleaseLabel='emr-5.9.0',
    Instances={
        'MasterInstanceType': instance_type,
        'SlaveInstanceType': instance_type,
        'InstanceCount': instance_count,
        'KeepJobFlowAliveWhenNoSteps': True,
        'Ec2KeyName': 'KeyPair',
        'EmrManagedSlaveSecurityGroup': 'sg-1234',
        'EmrManagedMasterSecurityGroup': 'sg-1234',
        'Ec2SubnetId': 'subnet-1q234',
    },
    Applications=[
        {'Name': 'Spark'},
        {'Name': 'Hadoop'}
    ],
    BootstrapActions=[
        {
            'Name': 'Install Python packages',
            'ScriptBootstrapAction': {
                'Path': 's3://mybucket/code/spark/bootstrap_spark_cluster.sh'
            }
        }
    ],
    VisibleToAllUsers=True,
    JobFlowRole='EMR_EC2_DefaultRole',
    ServiceRole='EMR_DefaultRole',
    Configurations=[
        {
            'Classification': 'spark',
            'Properties': {
                'maximizeResourceAllocation': 'true'
            }
        },
    ],
)

添加步骤

response = client.add_job_flow_steps(
    JobFlowId=cluster_id,
    Steps=[
        {
            'Name': 'Run Step',
            'ActionOnFailure': 'TERMINATE_CLUSTER',
            'HadoopJarStep': {
                'Args': [
                    'spark-submit',
                    '--deploy-mode', 'cluster',
                    '--py-files',
                    's3://mybucket/code/spark/spark_udfs.py',
                    's3://mybucket/code/spark/{}'.format(spark_script),
                    '--some-arg'
                ],
                'Jar': 'command-runner.jar'
            }
        }
    ]
)

这成功地添加了一个步骤并运行,但是,当步骤成功完成时,我希望集群自动终止,如AWS CLI中所述:http://docs.aws.amazon.com/cli/latest/reference/emr/create-cluster.html


Tags: instancenameclients3response步骤jobcode
1条回答
网友
1楼 · 发布于 2024-06-12 00:28:51

在您的例子中(使用boto3创建集群),您可以添加这些标志 'TerminationProtected': False, 'AutoTerminate': True,来创建集群。这样,在您完成运行集群的步骤后,集群将被关闭。

另一个解决方案是在要运行的步骤之后立即添加另一个步骤来终止集群。所以基本上你需要在步骤中运行这个命令

aws emr terminate-clusters --cluster-ids your_cluster_id

棘手的部分是检索集群id。 在这里您可以找到一些解决方案:Does an EMR master node know it's cluster id?

相关问题 更多 >