![](/img/trans.png)
[英]How to connect to RDS aurora serverless MYSQL private DB using Boto3?
[英]Boto3 rds-data continueAfterTimeout not supported in Aurora Serverless?
我正在嘗試使用 AWS rds-data api 在極光無服務器 Postgresql 中執行一些繁重的 etl。
根據AWS Rds DATA文檔, By default, a call times out if it's not finished processing within 45 seconds. However, you can continue running a SQL statement if the call times out by using the continueAfterTimeout parameter.
By default, a call times out if it's not finished processing within 45 seconds. However, you can continue running a SQL statement if the call times out by using the continueAfterTimeout parameter.
我看到boto3 rds-data支持 continueAfterTimeout 參數(布爾值)。 我可以在這樣的交易中使用這個標志。
def execute_transaction_query(sql, transaction_id):
print(sql)
response = rds_client.execute_statement(
secretArn=AURORA_DB_CREDS_ARN,
resourceArn=AURORA_DB_ARN,
database=AURORA_DB_NAME,
sql=sql,
transactionId=transaction_id,
continueAfterTimeout=True, # boolean flag to continue after timeout in theory
)
但是,查詢在 45 秒后仍然失敗並出現錯誤
An error occurred (StatementTimeoutException) when calling the ExecuteStatement operation: Request timed out
好的,所以 rds-data 調用失敗的原因是continueAfterTimeout=True
並不意味着 boto3 調用不會失敗,只是在數據庫上運行的 sql 查詢將繼續運行。
所以在運行 rds-data etls 時需要做的是在 try/catch 塊中執行語句:
response = rds_client.execute_statement(
secretArn=AURORA_DB_CREDS_ARN,
resourceArn=AURORA_DB_ARN,
database=AURORA_DB_NAME,
sql=sql,
transactionId=transaction_id,
continueAfterTimeout=True,
)
except botocore.exceptions.ClientError as error:
# aurora fails automatically after 45 seconds but continues in the db
#https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html
if error.response['Error']['Code'] == 'StatementTimeoutException':
print('QUERY TIMEDOUT AFTER MAX 45 SECONDS. THIS IS FINE')
# arbitrary wait in case the commit transaction fails with timeout
time.sleep(60)
else:
raise Exception(error)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.