gpt4 book ai didi

aws-lambda - (InvalidRequestException) 调用 GetQueryResults 时......从 Lambda Python 查询 Athena......无法读取结果

转载 作者:行者123 更新时间:2023-12-04 09:32:33 25 4
gpt4 key购买 nike

我一直在尝试从我的 lambda 函数 (Python3.8) 查询 Athena,但我一直收到相同的错误,尽管我尝试添加 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": "*"
}
]
}

这是我在日志中不断遇到的错误

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

"errorType": "InvalidRequestException"

"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)"]]}

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

最佳答案

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

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

关于aws-lambda - (InvalidRequestException) 调用 GetQueryResults 时......从 Lambda Python 查询 Athena......无法读取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62767533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com