Amazon Redshift non supporterà più la creazione di nuove UDF Python a partire dal 1º novembre 2025. Se desideri utilizzare le UDF Python, creale prima di tale data. Le UDF Python esistenti continueranno a funzionare normalmente. Per ulteriori informazioni, consulta il post del blog.
Utilizzo di GetStatementResult con un SDK AWS
Gli esempi di codice seguenti mostrano come utilizzare GetStatementResult.
Gli esempi di operazioni sono estratti di codice da programmi più grandi e devono essere eseguiti nel contesto. È possibile visualizzare questa operazione nel contesto nel seguente esempio di codice:
- Java
-
- SDK per Java 2.x
-
Controlla il risultato dell’istruzione.
/**
* 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
});
}
- Python
-
- SDK per Python (Boto3)
-
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
Il codice seguente crea un’istanza dell’oggetto RedshiftDataWrapper.
client = boto3.client("redshift-data")
redshift_data_wrapper = RedshiftDataWrapper(client)
Per un elenco completo delle guide per gli sviluppatori di SDK AWS ed esempi di codice, consulta la sezione Utilizzo del servizio con un SDK AWS. Questo argomento include anche informazioni su come iniziare e dettagli sulle versioni precedenti dell'SDK.