问题描述
我正在按照下面的给定代码使用boto3库执行Athena查询:
I am executing Athena query using the boto3 library as per the given code below:
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)
在执行此简单代码时,出现错误:
While executing this simple code I am getting the error:
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位置.
The thing I want to achieve is to get the result printed on console instead of getting it in store in s3 location.
P.S尽管有错误,输出仍存储在S3存储桶中.但是无法使用功能"get_query_results"
P.S the output is stored in S3 bucket despite the error. But not able to get the response using function "get_query_results"
推荐答案
在查询完成之前,即在状态为 SUCCEEDED
之前,无法获取查询结果.您必须使用 get_query_execution
API调用来轮询状态,直到状态为 SUCCEEDED
,然后才调用 get_query_results
.
There is no way to get query results until the query has completed, i.e. the state is SUCCEEDED
. You have to use the get_query_execution
API call to poll for the state, until it is SUCCEEDED
and only then call get_query_results
.
不幸的是,如果没有将结果存储在S3上,也无法获得结果.这是雅典娜的设计方式,没有办法解决.
There is also, unfortunately, no way to get the result without it being stored on S3. This is the way Athena is designed, and there is no way around it.
这篇关于使用boto3从python代码执行Athena查询会显示错误"botocore.errorfactory.InvalidRequestException".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!