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

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

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

GetStatementResult 搭配 a AWS SDK 使用

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

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

Java
Java 2.x 的 SDK
注意

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

檢查陳述式結果。

/** * Asynchronously retrieves the results of a statement execution. * * @param statementId the ID of the statement for which to retrieve the results * @return a {@link CompletableFuture} that completes when the statement result has been processed */ public CompletableFuture<Void> getResultsAsync(String statementId) { GetStatementResultRequest resultRequest = GetStatementResultRequest.builder() .id(statementId) .build(); return getAsyncDataClient().getStatementResult(resultRequest) .handle((response, exception) -> { if (exception != null) { logger.info("Error getting statement result {} ", exception.getMessage()); throw new RuntimeException("Error getting statement result: " + exception.getMessage(), exception); } // Extract and print the field values using streams if the response is valid. response.records().stream() .flatMap(List::stream) .map(Field::stringValue) .filter(value -> value != null) .forEach(value -> System.out.println("The Movie title field is " + value)); return response; }).thenAccept(response -> { // Optionally add more logic here if needed after handling the response }); }
  • 如需 API 詳細資訊,請參閱 GetStatementResult 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 get_statement_result(self, statement_id): """ Gets the result of a SQL statement. :param statement_id: The SQL statement identifier. :return: The SQL statement result. """ try: result = { "Records": [], } paginator = self.client.get_paginator("get_statement_result") for page in paginator.paginate(Id=statement_id): if "ColumnMetadata" not in result: result["ColumnMetadata"] = page["ColumnMetadata"] result["Records"].extend(page["Records"]) return result except ClientError as err: logging.error( "Couldn't get statement result. 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 詳細資訊,請參閱 GetStatementResult AWS SDK for Python (Boto3) Word 參考中的 API