提问者:小点点

(InvalidRequestException)调用GetQueryResults时。。。。。从Lambda Python查询Athena。。。。无法读取结果


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

下面是lambda函数:

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角色:

{
    "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": "*"
        }
    ]
}

这是我在日志中不断出现的错误

调用GetQuery结果操作时发生错误(InvalidRequest estException):查询未成功完成。最终查询状态:失败

“errorType”: “InvalidRequestException”

“堆栈跟踪”:[
[
“/var/task/lambda_function.py”,
26,
”function“,],[”/var/runtime/botocore/client.py“,316,”_api_call“,”return self._make_api_cll(operation_name,kwargs)“],[“/var/runtime/botocore/customer.py”,626,”_make_api_call“”,”raise error_class(parsed_response,operation_name)“]]}

如果有人能帮我,我会非常感激的——我已经试着解决这个问题好几天了


共1个答案

匿名用户

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

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