Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python boto EMR添加步骤和自动终止_Python_Amazon Web Services_Boto3_Emr - Fatal编程技术网

Python boto EMR添加步骤和自动终止

Python boto EMR添加步骤和自动终止,python,amazon-web-services,boto3,emr,Python,Amazon Web Services,Boto3,Emr,Python 2.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,

Python 2.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中所述:

在您的情况下(使用boto3创建群集),您可以添加这些标志
“TerminationProtected”:False,“AutoTerminate”:True,
用于集群创建。这样,在完成运行集群的步骤后,集群将被关闭

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

aws emr terminate-clusters --cluster-ids your_cluster_id aws emr终止群集--群集id您的\u群集\u id 棘手的部分是检索集群id。
在这里您可以找到一些解决方案:

建议的
'AutoTerminate':True
参数对我不起作用。但是,当我将参数“KeepJobFlowAliveWhenNoSteps”从
True
设置为
False
时,它就起作用了。您的代码应该如下所示:

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': False,
        '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'
            }
        },
    ],
)

通过在Instances参数中指定“KeepJobFlowAliveWhenNoSteps”:False,可以创建一个短期集群,该集群在所有步骤运行后自动终止。我已经添加了一个完整的示例来演示如何做到这一点

下面是演示中的一些代码:

def run_job_flow(
        name, log_uri, keep_alive, applications, job_flow_role, service_role,
        security_groups, steps, emr_client):
    try:
        response = emr_client.run_job_flow(
            Name=name,
            LogUri=log_uri,
            ReleaseLabel='emr-5.30.1',
            Instances={
                'MasterInstanceType': 'm5.xlarge',
                'SlaveInstanceType': 'm5.xlarge',
                'InstanceCount': 3,
                'KeepJobFlowAliveWhenNoSteps': keep_alive,
                'EmrManagedMasterSecurityGroup': security_groups['manager'].id,
                'EmrManagedSlaveSecurityGroup': security_groups['worker'].id,
            },
            Steps=[{
                'Name': step['name'],
                'ActionOnFailure': 'CONTINUE',
                'HadoopJarStep': {
                    'Jar': 'command-runner.jar',
                    'Args': ['spark-submit', '--deploy-mode', 'cluster',
                             step['script_uri'], *step['script_args']]
                }
            } for step in steps],
            Applications=[{
                'Name': app
            } for app in applications],
            JobFlowRole=job_flow_role.name,
            ServiceRole=service_role.name,
            EbsRootVolumeSize=10,
            VisibleToAllUsers=True
        )
        cluster_id = response['JobFlowId']
        logger.info("Created cluster %s.", cluster_id)
    except ClientError:
        logger.exception("Couldn't create cluster.")
        raise
    else:
        return cluster_id
下面是一些代码,使用一些实际参数调用此函数:

output_prefix = 'pi-calc-output'
pi_step = {
    'name': 'estimate-pi-step',
    'script_uri': f's3://{bucket_name}/{script_key}',
    'script_args':
        ['--partitions', '3', '--output_uri',
        f's3://{bucket_name}/{output_prefix}']
}
cluster_id = emr_basics.run_job_flow(
    f'{prefix}-cluster', f's3://{bucket_name}/logs',
    False, ['Hadoop', 'Hive', 'Spark'], job_flow_role, service_role,
    security_groups, [pi_step], emr_client)

也许我必须使用建议2,因为boto3似乎没有
自动终止
选项<代码>实例中的未知参数:“AutoTerminate”,必须是以下参数之一:MasterInstanceType、SlaveInstanceType、InstanceCount、InstanceGroups、Ec2KeyName、Placement、KeepJobFlowAliveWhenOsteps、TerminationProtected、HadoopVersion、Ec2SubnetId、EmrmagedMasterSecurityGroup、EmrmageDSLaveSecurityGroup、ServiceAccessSecurityGroup、,AdditionalMasterSecurityGroups,AdditionalSlaveSecurityGroups我在使用cloudformation时遇到了同样的问题。所以我使用了选项2,它工作得很好。实际上,我正在使用的步骤是调用一个lambda函数,该函数负责删除cloudformation堆栈(包含EMR集群)。谢谢,我将使用选项2。对于
boto3==1.4.8
,调用
运行作业流时,默认情况下,
自动终止
设置为
True
KeepJobFlowAliveWhenNostps
是更改此行为的参数-@Nobu Thank man这非常有用。当nosteps
设置为True时,我将
保持作业流有效,但我没有意识到这一点。这才是真正的解决办法。。。在我看来,这个答案有一个错误的解决方案,第二个更像是一个黑客。