有没有办法以编程方式获取通过 Redshift 数据 API 运行的查询的 Redshift 总数据扫描指标?

问题描述 投票:0回答:1

我的团队有一个 Lambda,可以触发从我们的 Redshift 集群运行的各种查询。

我可以通过选择查询执行来评估控制台中的查询,它会显示各种指标,但我特别对用于报告目的的“扫描的总数据”感兴趣:

我们已经根据 Redshift Data API 的 Describe Statement 的响应,从我的 Lambda 为每个查询类型发出了一些指标,但它只返回“结果行”和“结果大小”,而不返回有关的信息扫描的数据。虽然这些部分也很有用,但我们还需要跟踪扫描的总数据,因为它将帮助我们排除故障。

是否有类似的方法以编程方式获取此数据扫描信息?看起来可能有一个表我们可以查询信息,但我希望我们能以某种方式直接从 API 获取数据。

amazon-web-services amazon-redshift amazon-cloudwatch
1个回答
0
投票

您可以使用 boto3 SDK(Amazon 的 Python SDK)以编程方式针对 Redshift 运行 SQL 查询并获取结果。

下面的示例代码

import boto3

# Initialize Redshift client
client = boto3.client('redshift-data')

# SQL command
sql_command = """
SELECT query, SUM(bytes) AS total_bytes_scanned
FROM svl_query_report
WHERE query = [Your_Query_ID]
GROUP BY query;
"""

# Execute SQL command
response = client.execute_statement(
    ClusterIdentifier='your-redshift-cluster-id',
    Database='your-database-name',
    DbUser='your-db-username',
    Sql=sql_command,
    SecretArn='arn-of-your-secret-for-credentials'
)

# Fetch query execution status and results
query_id = response['Id']
status = None

while status not in ('FINISHED', 'FAILED', 'CANCELLED'):
    response_desc = client.describe_statement(Id=query_id)
    status = response_desc['Status']

if status == 'FINISHED':
    results = client.get_statement_result(Id=query_id)
    for record in results['Records']:
        print(record)

将 your-redshift-cluster-id、your-database-name、your-db-username、[Your_Query_ID] 和 arn-of-your-secret-for-credentials 等占位符替换为适当的值。

确保您已为 boto3 正确设置 AWS 凭证。您可以使用 AWS CLI、环境变量或 IAM 角色(如果在 EC2 实例或 Lambda 函数上运行)来执行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.