GetStatementResult搭配使用 AWS SDK - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

GetStatementResult搭配使用 AWS SDK

以下代码示例演示如何使用 GetStatementResult

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 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详细信息,请参阅 “AWS SDK for Java 2.x API参考 GetStatementResult” 中的。

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 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 SDKPython (Boto3) API 参考。