要解决athena.getQueryExecution在1分钟以上返回一个正在运行的状态,尽管查询历史显示作业只花费了4秒钟的问题,可以尝试以下方法:
import boto3
import time
athena = boto3.client('athena')
query_execution_id = "YOUR_QUERY_EXECUTION_ID"
response = athena.get_query_execution(QueryExecutionId=query_execution_id)
status = response['QueryExecution']['Status']['State']
start_time = time.time()
while status in ['QUEUED', 'RUNNING']:
response = athena.get_query_execution(QueryExecutionId=query_execution_id)
status = response['QueryExecution']['Status']['State']
elapsed_time = time.time() - start_time
if elapsed_time > 60:
break
time.sleep(1)
if status == 'SUCCEEDED':
print("Query completed successfully!")
else:
print("Query did not complete within 1 minute.")
import boto3
import time
athena = boto3.client('athena')
query_execution_id = "YOUR_QUERY_EXECUTION_ID"
response = athena.get_query_execution(QueryExecutionId=query_execution_id)
status = response['QueryExecution']['Status']['State']
start_time = time.time()
while status in ['QUEUED', 'RUNNING']:
response = athena.get_query_execution(QueryExecutionId=query_execution_id)
status = response['QueryExecution']['Status']['State']
elapsed_time = time.time() - start_time
if elapsed_time > 60:
break
time.sleep(1)
# 确保查询已经完成
response = athena.get_query_execution(QueryExecutionId=query_execution_id)
status = response['QueryExecution']['Status']['State']
if status == 'SUCCEEDED':
print("Query completed successfully!")
else:
print("Query did not complete within 1 minute.")
通过以上方法,你可以确保在1分钟内获取到正确的查询状态,以便进一步处理查询结果。需要注意的是,上述示例中的"YOUR_QUERY_EXECUTION_ID"需要替换为实际的查询ID。