gpt4 book ai didi

boto3 - 使用 boto3 从 python 代码执行 Athena Query 显示错误 "botocore.errorfactory.InvalidRequestException"

转载 作者:行者123 更新时间:2023-12-05 00:58:59 25 4
gpt4 key购买 nike

我正在按照下面给定的代码使用 boto3 库执行 Athena 查询:

import boto3
client = boto3.client('athena')

def main():
queryStart = client.start_query_execution(
QueryString = 'SELECT * FROM <tablename>',
QueryExecutionContext = {
'Database': '<databasename>'
},
ResultConfiguration={
'OutputLocation': 's3://<outputlocation>',
'EncryptionConfiguration': {
'EncryptionOption': 'SSE_S3'
}
}
)
queryExecution = client.get_query_results(QueryExecutionId=queryStart['QueryExecutionId'],MaxResults=10)
prinnt(queryExecution)

执行此简单代码时出现错误:

  Traceback (most recent call last):
File "readingathena.py", line 38, in <module>
main()
File "readingathena.py", line 33, in main
for i in response_iterator:
File "C:\Program Files\Python36\lib\site-packages\botocore\paginate.py", line 255, in __iter__
response = self._make_request(current_kwargs)
File "C:\Program Files\Python36\lib\site-packages\botocore\paginate.py", line 332, in _make_request
return self._method(**current_kwargs)
File "C:\Program Files\Python36\lib\site-packages\botocore\client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "C:\Program Files\Python36\lib\site-packages\botocore\client.py", line 661, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.InvalidRequestException: An error occurred (InvalidRequestException) when calling the GetQueryResults operation: Query has not yet finished. Current state: RUNNING

我想要实现的是将结果打印在控制台上,而不是将其存储在 s3 位置。

P.S 尽管出现错误,输出仍存储在 S3 存储桶中。但无法使用函数 "get_query_results"

获得响应

最佳答案

在查询完成之前无法获取查询结果,即状态为 SUCCEEDED。您必须使用 get_query_execution API 调用来轮询状态,直到状态为 SUCCEEDED,然后才调用 get_query_results

不幸的是,如果不将结果存储在 S3 上,就无法获得结果。这就是 Athena 的设计方式,没有办法绕过它。

关于boto3 - 使用 boto3 从 python 代码执行 Athena Query 显示错误 "botocore.errorfactory.InvalidRequestException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55058618/

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