(InvalidRequestException) 调用 GetQueryResults 时..... 从 Lambda Python 查询 Athena.. 无法读取结果 [英] (InvalidRequestException) when calling the GetQueryResults..... Querying Athena From Lambda Python.... Cannot Read Results

查看:26
本文介绍了(InvalidRequestException) 调用 GetQueryResults 时..... 从 Lambda Python 查询 Athena.. 无法读取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从我的 lambda 函数 (Python3.8) 中查询 Athena,但我不断收到相同的错误,尽管我尝试添加 if else 语句来检查执行状态,并且我总是在 aws 控制台上出现相同的错误和本地的 cli

I have been trying to query Athena from my lambda function (Python3.8) but I keep getting the same error although tried adding an if else statement to check the status of the execution and i always the same error on the aws console and the cli locally

这里是 lambda 函数:

Here is the lambda function:

import json
import boto3
import time

def function(event, context):
    client=boto3.client('athena')

    #setup and perform query
    queryStart=client.start_query_execution(
        QueryString = 'SELECT * FROM my_s3_bucket_developer limit 8;',
        QueryExecutionContext = {
            'Database':'mydb'
        },
        ResultConfiguration = {
            'OutputLocation': 's3://athena-results-queries-developer/'
        }
    ) 
    
    #get query ID
    queryId= queryStart['QueryExecutionId']

    #we gonna sleep the function now because we don't know how 
    #long it will take to execute the query
    time.sleep(25)

    results=client.get_query_results(QueryExecutionId = queryId)
    for row in results['ResultSet']['Rows']:
        print(row)

这是我附加到我的 lambda 函数的 IAM 角色:

and this is the IAM Role I have attached to my lambda function:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "S3:GetBucketLocation",
                "S3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::athena-results-queries-developer/*",
            ]
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "athena:StartQueryExecution",
                "athena:StopQueryExecution",
                "athena:GetQueryExecution",
                "athena:GetQueryResults",
                "glue:GetTable"
            ],
            "Resource": "*"
        }
    ]
}

这是我一直在日志中出现的错误

This is the error I keep getting in the logs

调用 GetQueryResults 操作时发生错误 (InvalidRequestException):查询未成功完成.最终查询状态:FAILED

An error occurred (InvalidRequestException) when calling the GetQueryResults operation: Query did not finish successfully. Final query state: FAILED

errorType":InvalidRequestException"

"errorType": "InvalidRequestException"

堆栈跟踪":[
[
/var/task/lambda_function.py",
26、
功能",
results=client.get_query_results(QueryExecutionId = queryId)"
],[/var/runtime/botocore/client.py",316,_api_call",返回 self._make_api_call(operation_name, kwargs)";],[/var/runtime/botocore/client.py",626,_make_api_call",提高 error_class(parsed_response, operation_name)";]]}

"stackTrace": [
[
"/var/task/lambda_function.py",
26,
"function",
"results=client.get_query_results(QueryExecutionId = queryId)"
], [ "/var/runtime/botocore/client.py", 316, "_api_call", "return self._make_api_call(operation_name, kwargs)" ], [ "/var/runtime/botocore/client.py", 626, "_make_api_call", "raise error_class(parsed_response, operation_name)" ] ] }

如果有人可以帮助我,我将不胜感激 - 我已经尝试解决这个问题好几天了

If anyone can help me would really appreciate it - I have been trying to solve this for days

推荐答案

问题在于您没有等待查询正确完成.在调用 get_query_results 之前,您需要调用 get_query_execution 并检查查询是否成功.

The problem is that you don't wait for the query to complete properly. You need to call get_query_execution and check that the query has succeeded before you call get_query_results.

这里有一个完整的示例,您可以从中获得灵感:https://www.ilkkapeltola.fi/2018/04/simple-way-to-query-amazon-athena-in.html

There's a full example here that you could take inspiration from: https://www.ilkkapeltola.fi/2018/04/simple-way-to-query-amazon-athena-in.html

这篇关于(InvalidRequestException) 调用 GetQueryResults 时..... 从 Lambda Python 查询 Athena.. 无法读取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆