DescribeStatement 搭配 a AWS SDK 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DescribeStatement 搭配 a AWS SDK 使用

下列程式碼範例示範如何使用 DescribeStatement

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

Java
Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Checks the status of an SQL statement asynchronously and handles the completion of the statement. * * @param sqlId the ID of the SQL statement to check * @return a {@link CompletableFuture} that completes when the SQL statement's status is either "FINISHED" or "FAILED" */ public CompletableFuture<Void> checkStatementAsync(String sqlId) { DescribeStatementRequest statementRequest = DescribeStatementRequest.builder() .id(sqlId) .build(); return getAsyncDataClient().describeStatement(statementRequest) .thenCompose(response -> { String status = response.statusAsString(); logger.info("... Status: {} ", status); if ("FAILED".equals(status)) { throw new RuntimeException("The Query Failed. Ending program"); } else if ("FINISHED".equals(status)) { return CompletableFuture.completedFuture(null); } else { // Sleep for 1 second and recheck status return CompletableFuture.runAsync(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { throw new RuntimeException("Error during sleep: " + e.getMessage(), e); } }).thenCompose(ignore -> checkStatementAsync(sqlId)); // Recursively call until status is FINISHED or FAILED } }).whenComplete((result, exception) -> { if (exception != null) { // Handle exceptions logger.info("Error: {} ", exception.getMessage()); } else { logger.info("The statement is finished!"); } }); }
  • 如需 API 詳細資訊,請參閱 DescribeStatement AWS SDK for Java 2.x 參考中的 API

Python
SDK for Python (Boto3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class RedshiftDataWrapper: """Encapsulates Amazon Redshift data.""" def __init__(self, client): """ :param client: A Boto3 RedshiftDataWrapper client. """ self.client = client def describe_statement(self, statement_id): """ Describes a SQL statement. :param statement_id: The SQL statement identifier. :return: The SQL statement result. """ try: response = self.client.describe_statement(Id=statement_id) return response except ClientError as err: logging.error( "Couldn't describe statement. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise

下列程式碼會實例化 RedshiftDataWrapper 物件。

client = boto3.client("redshift-data") redshift_data_wrapper = RedshiftDataWrapper(client)
  • 如需 API 詳細資訊,請參閱 DescribeStatement AWS SDK for Python (Boto3) Word 參考中的 API