【问题标题】:List all "Active" EMR cluster using Boto3使用 Boto3 列出所有“活动”EMR 集群
【发布时间】:2019-07-07 17:42:26
【问题描述】:

我正在尝试使用 boto3 列出 EMR 上的所有活动集群,但我的代码似乎无法正常工作,它只是返回 null。

我正在尝试使用 boto3 来做到这一点

1) 列出所有 Active EMR 集群

aws emr list-clusters --active

2) 仅列出集群 id 和 Active 的名称 集群名称

aws emr list-clusters --active --query "Clusters[*].{Name:Name}" --output text

集群 ID

aws emr list-clusters --active --query "Clusters[*].{ClusterId:Id}" --output text

但是我在使用boto3的开始阶段被阻止了

import boto3
client = boto3.client("emr")
response = client.list_clusters(
    ClusterStates=[
        'STARTING',
    ],
)

print response

任何建议我如何将这些 CLI 命令转换为 boto3

谢谢

【问题讨论】:

  • 你是怎么被屏蔽的? CLI 的 --active 的 boto3 等效项可能类似于 ClusterStates=['STARTING', 'BOOTSTRAPPING', 'RUNNING', 'WAITING', 'TERMINATING']。
  • 您可能只想获取完整的集群列表(没有过滤器),然后在代码中缩小列表范围。

标签: python amazon-web-services boto3


【解决方案1】:

以下代码可以打印激活的emr名称和id:

import boto3
client = boto3.client("emr")
response = client.list_clusters(
    ClusterStates=[
        'STARTING', 'BOOTSTRAPPING', 'RUNNING', 'WAITING', 'TERMINATING'
    ]
)
for cluster in response['Clusters']:
    print(cluster['Name'])
    print(cluster['Id'])

【讨论】:

    【解决方案2】:

    @shifu.zheng 的回答略有更新:

    import boto3
    client = boto3.client("emr",region_name = "us-west-1", aws_access_key_id = "sdufashdifos123121", aws_secret_access_key ="sjdfnsaldfoasd1231312")
    response = client.list_clusters(ClusterStates=['STARTING', 'BOOTSTRAPPING', 'RUNNING', 'WAITING', 'TERMINATING'])
    for cluster in response['Clusters']:
    print(cluster['Name'])
    print(cluster['Id'])
    

    您需要在 boto3.client 对象中有所需的参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多