【问题标题】:AWSCLI Commands using Python使用 Python 的 AWSCLI 命令
【发布时间】:2022-01-10 02:19:15
【问题描述】:

我想获取活动 EMR 集群的 ClusterId、ClusterArn、Public DNS 并将它们加载到 Postgres 表中。我可以在控制台中使用 CLI 命令获取 ClusterId 和 Arn。

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

获得 cluster_id 后,我可以使用 CLI 命令获取 DNS。

cluster_id=j-xxx
aws emr describe-cluster --output text --cluster-id $cluster_id --query Cluster.MasterPublicDnsName

但我必须在 Python 脚本中执行此操作。 我无法将此命令集成到 python 脚本中。 所以为了我的目的,我做了以下事情 - 运行以下命令并将输出重定向到 json 文件。

aws emr list-clusters --active > test.json

test.json 文件的内容 -

{
    "Clusters": [
        {
            "Id": "j-xxx",
            "Name": "xxx",
            "Status": {
                "State": "WAITING",
                "StateChangeReason": {
                    "Message": "Cluster ready after last step completed."
                },
                "Timeline": {
                    "CreationDateTime": "2021-12-01T01:08:10.755000-06:00",
                    "ReadyDateTime": "2021-12-01T01:20:13.483000-06:00"
                }
            },
            "NormalizedInstanceHours": 832,
            "ClusterArn": "arn:aws:elasticmapreduce:xxx:xxx:cluster/j-xxx"
        }
    ]
}

现在使用 Python 读取该 json 文件 -

import json
import psycopg2

with open("cluster_info.json") as file:
    data=json.load(file)
CId=data["Clusters"][0]["Id"]
CArn=data["Clusters"][0]["ClusterArn"]
print(CId)
print(CArn)
#CDNS=`aws emr describe-cluster --output text --cluster-id $CId --query Cluster.MasterPublicDnsName`
#print(CDNS)
conn = psycopg2.connect(
   database="postgres", user='xxx', password='xxx', host='xxxx.rds.amazonaws.com', port= '5432'
)
cursor = conn.cursor()
query = '''INSERT INTO STAGE.EMR_CLUSTER_INFO (Cluster_ID, Cluster_Arn, Public_DNS) VALUES (%s,%s,%s)'''
values = (CId, CArn, 'ip-xxxx.ec2.internal') #Since I wasnt able to fetch DNS,so hardcoded the value just to test if the record is getting inserted in the tale or not
cursor.execute(query,values)
conn.commit()
print("Records inserted........")
conn.close()

我能够在表格中插入记录。 但我需要在同一个脚本中获取 ClusterID、Arn、DNS,然后加载表中的值。 尝试使用 Boto3 ...无法成功 ...请帮助。 提前致谢。

【问题讨论】:

  • 您应该从 Python 调用 AWS CLI。请改用 boto3,这是适用于 Python 的 AWS 开发工具包。

标签: python amazon-web-services boto3 aws-cli amazon-emr


【解决方案1】:

来自boto3describe_cluster()

import boto3

emr_client = boto3.client('emr')

clusters = emr_client.list_clusters()

for cluster in clusters['Clusters']:

  cluster_id = cluster['Id']

  response = emr_client.describe_cluster(ClusterId=cluster_id)

  cluster_arn = response['Cluster']['ClusterArn']
  cluster_dns_name = response['Cluster']['MasterPublicDnsName']

  # Insert into database here

【讨论】:

  • 感谢约翰的时间和帮助,它奏效了。非常感谢。
【解决方案2】:

这就是你要找的吗? How do I list all running EMR clusters using Boto?https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/emr.html#client

您可以从 boto 获取 MasterPublicDnsName、Id 和 clusterARN 的详细信息。如果您需要了解更多信息,请告诉我。

【讨论】:

  • 感谢您的回复。是的,我已经浏览了这些链接,但是我需要一个示例……我可以在其中获取所需的值(可能到变量中),然后使用我的插入语句中的变量加载到表中。我能够运行这些单独的 aws cli 命令并获取输出,无法通过 Python 运行它们。感谢您的时间和帮助。
猜你喜欢
  • 2015-12-26
  • 1970-01-01
  • 2014-07-09
  • 2020-04-01
  • 1970-01-01
  • 2018-03-21
  • 1970-01-01
  • 2020-05-05
  • 2017-01-27
相关资源
最近更新 更多