DeleteCluster 搭配 AWS SDK或 使用 CLI - Amazon Redshift

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

DeleteCluster 搭配 AWS SDK或 使用 CLI

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

CLI
AWS CLI

刪除沒有最終叢集 SnapshotThis 範例的叢集會刪除叢集,強制刪除資料,因此不會建立最終叢集快照。命令:

aws redshift delete-cluster --cluster-identifier mycluster --skip-final-cluster-snapshot

刪除叢集,允許最終叢集 SnapshotThis 範例會刪除叢集,但會指定最終叢集快照。命令:

aws redshift delete-cluster --cluster-identifier mycluster --final-cluster-snapshot-identifier myfinalsnapshot
  • 如需API詳細資訊,請參閱 命令參考 DeleteCluster中的 。 AWS CLI

Go
SDK for Go V2
注意

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

import ( "context" "errors" "log" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/redshift" "github.com/aws/aws-sdk-go-v2/service/redshift/types" ) // RedshiftActions wraps Redshift service actions. type RedshiftActions struct { RedshiftClient *redshift.Client } // DeleteCluster deletes the given cluster. func (actor RedshiftActions) DeleteCluster(ctx context.Context, clusterId string) (bool, error) { input := redshift.DeleteClusterInput{ ClusterIdentifier: aws.String(clusterId), SkipFinalClusterSnapshot: aws.Bool(true), } _, err := actor.RedshiftClient.DeleteCluster(ctx, &input) var opErr *types.ClusterNotFoundFault if err != nil && errors.As(err, &opErr) { log.Println("Cluster was not found. Where could it be?") return false, err } else if err != nil { log.Printf("Failed to delete Redshift cluster: %v\n", err) return false, err } waiter := redshift.NewClusterDeletedWaiter(actor.RedshiftClient) err = waiter.Wait(ctx, &redshift.DescribeClustersInput{ ClusterIdentifier: aws.String(clusterId), }, 5*time.Minute) if err != nil { log.Printf("Wait time exceeded for deleting cluster, continuing: %v\n", err) } log.Printf("The cluster %s was deleted\n", clusterId) return true, nil }
  • 如需API詳細資訊,請參閱 參考 DeleteCluster中的 。 AWS SDK for Go API

Java
SDK 適用於 Java 2.x
注意

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

刪除叢集。

/** * Deletes a Redshift cluster asynchronously. * * @param clusterId the identifier of the Redshift cluster to be deleted * @return a {@link CompletableFuture} that represents the asynchronous operation of deleting the Redshift cluster */ public CompletableFuture<DeleteClusterResponse> deleteRedshiftClusterAsync(String clusterId) { DeleteClusterRequest deleteClusterRequest = DeleteClusterRequest.builder() .clusterIdentifier(clusterId) .skipFinalClusterSnapshot(true) .build(); return getAsyncClient().deleteCluster(deleteClusterRequest) .whenComplete((response, exception) -> { if (exception != null) { // Handle exceptions if (exception.getCause() instanceof RedshiftException) { logger.info("Error: {}", exception.getMessage()); } else { logger.info("Unexpected error: {}", exception.getMessage()); } } else { // Handle successful response logger.info("The status is {}", response.cluster().clusterStatus()); } }); }
  • 如需API詳細資訊,請參閱 參考 DeleteCluster中的 。 AWS SDK for Java 2.x API

JavaScript
SDK 適用於 JavaScript (v3)
注意

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

建立用戶端。

import { RedshiftClient } from "@aws-sdk/client-redshift"; // Set the AWS Region. const REGION = "REGION"; //Set the Redshift Service Object const redshiftClient = new RedshiftClient({ region: REGION }); export { redshiftClient };

建立 叢集

// Import required AWS SDK clients and commands for Node.js import { DeleteClusterCommand } from "@aws-sdk/client-redshift"; import { redshiftClient } from "./libs/redshiftClient.js"; const params = { ClusterIdentifier: "CLUSTER_NAME", SkipFinalClusterSnapshot: false, FinalClusterSnapshotIdentifier: "CLUSTER_SNAPSHOT_ID", }; const run = async () => { try { const data = await redshiftClient.send(new DeleteClusterCommand(params)); console.log("Success, cluster deleted. ", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 如需API詳細資訊,請參閱 參考 DeleteCluster中的 。 AWS SDK for JavaScript API

Kotlin
SDK 適用於 Kotlin
注意

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

刪除叢集。

suspend fun deleteRedshiftCluster(clusterId: String?) { val request = DeleteClusterRequest { clusterIdentifier = clusterId skipFinalClusterSnapshot = true } RedshiftClient { region = "us-west-2" }.use { redshiftClient -> val response = redshiftClient.deleteCluster(request) println("The status is ${response.cluster?.clusterStatus}") } }
  • 如需API詳細資訊,請參閱DeleteCluster中的 AWS SDK for Kotlin API參考

Python
SDK for Python (Boto3)
注意

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

class RedshiftWrapper: """ Encapsulates Amazon Redshift cluster operations. """ def __init__(self, redshift_client): """ :param redshift_client: A Boto3 Redshift client. """ self.client = redshift_client def delete_cluster(self, cluster_identifier): """ Deletes a cluster. :param cluster_identifier: The cluster identifier. """ try: self.client.delete_cluster( ClusterIdentifier=cluster_identifier, SkipFinalClusterSnapshot=True ) except ClientError as err: logging.error( "Couldn't delete a cluster. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise

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

client = boto3.client("redshift") redhift_wrapper = RedshiftWrapper(client)
  • 如需API詳細資訊,請參閱 DeleteCluster 中的 AWS SDK for Python (Boto3) API參考

如需開發人員指南和程式碼範例的完整清單 AWS SDK,請參閱 使用此服務 AWS SDK。本主題也包含有關入門的資訊,以及先前SDK版本的詳細資訊。