本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 AWS SDKs
AWS SDKs 以您偏好的程式設計語言提供對 Aurora DSQL 的程式設計存取。下列各節說明如何使用不同的程式設計語言執行常見的叢集操作。
建立叢集
下列範例示範如何使用不同的程式設計語言建立單一區域叢集。
- Python
-
若要在單一 中建立叢集 AWS 區域,請使用下列範例。
import boto3 def create_cluster(region): try: client = boto3.client("dsql", region_name=region) tags = {"Name": "Python single region cluster"} cluster = client.create_cluster(tags=tags, deletionProtectionEnabled=True) print(f"Initiated creation of cluster: {cluster["identifier"]}") print(f"Waiting for {cluster["arn"]} to become ACTIVE") client.get_waiter("cluster_active").wait( identifier=cluster["identifier"], WaiterConfig={ 'Delay': 10, 'MaxAttempts': 30 } ) return cluster except: print("Unable to create cluster") raise def main(): region = "us-east-1" response = create_cluster(region) print(f"Created cluster: {response["arn"]}") if __name__ == "__main__": main()
- C++
-
下列範例可讓您在單一 中建立叢集 AWS 區域。
#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/dsql/DSQLClient.h> #include <aws/dsql/model/CreateClusterRequest.h> #include <aws/dsql/model/GetClusterRequest.h> #include <iostream> #include <thread> #include <chrono> using namespace Aws; using namespace Aws::DSQL; using namespace Aws::DSQL::Model; /** * Creates a single-region cluster in Amazon Aurora DSQL */ CreateClusterResult CreateCluster(const Aws::String& region) { // Create client for the specified region DSQL::DSQLClientConfiguration clientConfig; clientConfig.region = region; DSQL::DSQLClient client(clientConfig); // Create the cluster CreateClusterRequest createClusterRequest; createClusterRequest.SetDeletionProtectionEnabled(true); createClusterRequest.SetClientToken(Aws::Utils::UUID::RandomUUID()); // Add tags Aws::Map<Aws::String, Aws::String> tags; tags["Name"] = "cpp single region cluster"; createClusterRequest.SetTags(tags); auto createOutcome = client.CreateCluster(createClusterRequest); if (!createOutcome.IsSuccess()) { std::cerr << "Failed to create cluster in " << region << ": " << createOutcome.GetError().GetMessage() << std::endl; throw std::runtime_error("Unable to create cluster in " + region); } auto cluster = createOutcome.GetResult(); std::cout << "Created " << cluster.GetArn() << std::endl; return cluster; } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { try { // Define region for the single-region setup Aws::String region = "us-east-1"; auto cluster = CreateCluster(region); std::cout << "Created single region cluster:" << std::endl; std::cout << "Cluster ARN: " << cluster.GetArn() << std::endl; std::cout << "Cluster Status: " << ClusterStatusMapper::GetNameForClusterStatus(cluster.GetStatus()) << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } } Aws::ShutdownAPI(options); return 0; }
- JavaScript
-
若要在單一 中建立叢集 AWS 區域,請使用下列範例。
import { DSQLClient, CreateClusterCommand, waitUntilClusterActive } from "@aws-sdk/client-dsql"; async function createCluster(region) { const client = new DSQLClient({ region }); try { const createClusterCommand = new CreateClusterCommand({ deletionProtectionEnabled: true, tags: { Name: "javascript single region cluster" }, }); const response = await client.send(createClusterCommand); console.log(`Waiting for cluster ${response.identifier} to become ACTIVE`); await waitUntilClusterActive( { client: client, maxWaitTime: 300 // Wait for 5 minutes }, { identifier: response.identifier } ); console.log(`Cluster Id ${response.identifier} is now active`); return; } catch (error) { console.error(`Unable to create cluster in ${region}: `, error.message); throw error; } } async function main() { const region = "us-east-1"; await createCluster(region); } main();
- Java
-
使用下列範例在單一 中建立叢集 AWS 區域。
package org.example; import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.retries.api.BackoffStrategy; import software.amazon.awssdk.services.dsql.DsqlClient; import software.amazon.awssdk.services.dsql.model.CreateClusterRequest; import software.amazon.awssdk.services.dsql.model.CreateClusterResponse; import software.amazon.awssdk.services.dsql.model.GetClusterResponse; import java.time.Duration; import java.util.Map; public class CreateCluster { public static void main(String[] args) { Region region = Region.US_EAST_1; try ( DsqlClient client = DsqlClient.builder() .region(region) .credentialsProvider(DefaultCredentialsProvider.create()) .build() ) { CreateClusterRequest request = CreateClusterRequest.builder() .deletionProtectionEnabled(true) .tags(Map.of("Name", "java single region cluster")) .build(); CreateClusterResponse cluster = client.createCluster(request); System.out.println("Created " + cluster.arn()); // The DSQL SDK offers a built-in waiter to poll for a cluster's // transition to ACTIVE. System.out.println("Waiting for cluster to become ACTIVE"); WaiterResponse<GetClusterResponse> waiterResponse = client.waiter().waitUntilClusterActive( getCluster -> getCluster.identifier(cluster.identifier()), config -> config.backoffStrategyV2( BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(10)) ).waitTimeout(Duration.ofMinutes(5)) ); waiterResponse.matched().response().ifPresent(System.out::println); } } }
- Rust
-
若要在單一 中建立叢集 AWS 區域,請使用下列範例。
use aws_config::{BehaviorVersion, Region, load_defaults}; use aws_sdk_dsql::client::Waiters; use aws_sdk_dsql::operation::get_cluster::GetClusterOutput; use aws_sdk_dsql::{Client, Config}; use std::collections::HashMap; /// Create a client. We will use this later for performing operations on the cluster. async fn dsql_client(region: &'static str) -> Client { let region_provider = Region::new(region); let config = load_defaults(BehaviorVersion::latest()) .region(region_provider) .load() .await; let config = Config::new(&config); Client::from_conf(config) } /// Create a cluster without delete protection and a name pub async fn create_cluster(region: &'static str) -> GetClusterOutput { let client = dsql_client(region).await; let tags = HashMap::from([ (String::from("Name"), String::from("rust single region cluster")), ]); println!("Creating cluster in {region}"); let cluster = client .create_cluster() .set_tags(Some(tags)) .deletion_protection_enabled(true) .send() .await .unwrap(); println!("Created {}", cluster.arn); println!("Waiting for {} to become ACTIVE", cluster.arn); let cluster_output = client .wait_until_cluster_active() .identifier(&cluster.identifier) .send() .await .unwrap(); cluster_output } #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let region = "us-east-1"; let cluster = create_cluster(region).await; println!("Created single region cluster:"); println!("{:#?}", cluster); Ok(()) }
- Ruby
-
若要在單一 中建立叢集 AWS 區域,請使用下列範例。
require "aws-sdk-dsql" require "pp" def create_cluster(region) client = Aws::DSQL::Client.new(region: region) puts "Creating cluster in #{region}" cluster = client.create_cluster( deletion_protection_enabled: true, tags: { Name: "ruby single region cluster" } ) puts "Created #{cluster.arn}" puts "Waiting for #{cluster.arn} to become ACTIVE" cluster = client.wait_until(:cluster_active, identifier: cluster.identifier) do |w| # Wait for 5 minutes w.max_attempts = 30 w.delay = 10 end cluster rescue Aws::Errors::ServiceError => e abort "Failed to create cluster: #{e.message}" end def main region = "us-east-1" cluster = create_cluster(region) puts "Created single region cluster:" pp cluster end main if $PROGRAM_NAME == __FILE__
- .NET
-
若要在單一 中建立叢集 AWS 區域,請使用下列範例。
using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon; using Amazon.DSQL; using Amazon.DSQL.Model; using Amazon.Runtime; using Amazon.Runtime.Credentials; using Amazon.Runtime.Endpoints; namespace DSQLExamples.examples { public class CreateCluster { /// <summary> /// Create a client. We will use this later for performing operations on the cluster. /// </summary> private static async Task<AmazonDSQLClient> CreateDSQLClient(RegionEndpoint region) { var awsCredentials = new DefaultAWSCredentialsChain().GetCredentials(); var clientConfig = new AmazonDSQLConfig { RegionEndpoint = region }; return new AmazonDSQLClient(awsCredentials, clientConfig); } /// <summary> /// Create a cluster with deletion protection enabled and a name tag. /// </summary> public static async Task<CreateClusterResponse> Create(RegionEndpoint region) { using (var client = await CreateDSQLClient(region)) { var tags = new Dictionary<string, string> { { "Name", "csharp single region cluster" } }; var createClusterRequest = new CreateClusterRequest { DeletionProtectionEnabled = true, Tags = tags }; var cluster = await client.CreateClusterAsync(createClusterRequest); Console.WriteLine($"Created {cluster.Arn}"); return cluster; } } public static async Task Main() { var region = RegionEndpoint.USEast1; var cluster = await Create(region); Console.WriteLine("Created single region cluster:"); Console.WriteLine($"Cluster ARN: {cluster.Arn}"); } } }
- Golang
-
若要在單一 中建立叢集 AWS 區域,請使用下列範例。
package main import ( "context" "fmt" "log" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/dsql" ) func CreateCluster(ctx context.Context, region string) error { cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region)) if err != nil { log.Fatalf("Failed to load AWS configuration: %v", err) } client := dsql.NewFromConfig(cfg) deleteProtect := true input := &dsql.CreateClusterInput{ DeletionProtectionEnabled: &deleteProtect, Tags: map[string]string{ "Name": "go single-region cluster", }, } clusterProperties, err := client.CreateCluster(context.Background(), input) if err != nil { return fmt.Errorf("failed to create cluster. %v", err) } // Create the waiter with our custom options waiter := dsql.NewClusterActiveWaiter(client, func(o *dsql.ClusterActiveWaiterOptions) { o.MaxDelay = 30 * time.Second o.MinDelay = 10 * time.Second o.LogWaitAttempts = true }) // Create the input for the clusterProperties to monitor clusterInput := &dsql.GetClusterInput{ Identifier: clusterProperties.Identifier, } fmt.Printf("Waiting for cluster %s to become ACTIVE\n", *clusterProperties.Arn) err = waiter.Wait(ctx, clusterInput, 5*time.Minute) if err != nil { return fmt.Errorf("error waiting for cluster to become active: %w", err) } fmt.Printf("Created single region cluster: %s\n", *clusterProperties.Arn) return nil } func main() { // Set up context with timeout ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) defer cancel() err := CreateCluster(ctx, "us-east-1") if err != nil { fmt.Printf("failed to create cluster: %v", err) panic(err) } }
取得叢集
下列範例示範如何使用不同的程式設計語言取得單一區域叢集的相關資訊。
- Python
-
若要取得單一區域叢集的相關資訊,請使用下列範例。
import boto3 from datetime import datetime import json def get_cluster(region, identifier): try: client = boto3.client("dsql", region_name=region) return client.get_cluster(identifier=identifier) except: print(f"Unable to get cluster {identifier} in region {region}") raise def main(): region = "us-east-1" cluster_id = "<your cluster id>" response = get_cluster(region, cluster_id) print(json.dumps(response, indent=2, default=lambda obj: obj.isoformat() if isinstance(obj, datetime) else None)) if __name__ == "__main__": main()
- C++
-
使用下列範例取得單一區域叢集的相關資訊。
#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/dsql/DSQLClient.h> #include <aws/dsql/model/GetClusterRequest.h> #include <iostream> using namespace Aws; using namespace Aws::DSQL; using namespace Aws::DSQL::Model; /** * Retrieves information about a cluster in Amazon Aurora DSQL */ GetClusterResult GetCluster(const Aws::String& region, const Aws::String& identifier) { // Create client for the specified region DSQL::DSQLClientConfiguration clientConfig; clientConfig.region = region; DSQL::DSQLClient client(clientConfig); // Get the cluster GetClusterRequest getClusterRequest; getClusterRequest.SetIdentifier(identifier); auto getOutcome = client.GetCluster(getClusterRequest); if (!getOutcome.IsSuccess()) { std::cerr << "Failed to retrieve cluster " << identifier << " in " << region << ": " << getOutcome.GetError().GetMessage() << std::endl; throw std::runtime_error("Unable to retrieve cluster " + identifier + " in region " + region); } return getOutcome.GetResult(); } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { try { // Define region and cluster ID Aws::String region = "us-east-1"; Aws::String clusterId = "<your cluster id>"; auto cluster = GetCluster(region, clusterId); // Print cluster details std::cout << "Cluster Details:" << std::endl; std::cout << "ARN: " << cluster.GetArn() << std::endl; std::cout << "Status: " << ClusterStatusMapper::GetNameForClusterStatus(cluster.GetStatus()) << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } } Aws::ShutdownAPI(options); return 0; }
- JavaScript
-
若要取得單一區域叢集的相關資訊,請使用下列範例。
import { DSQLClient, GetClusterCommand } from "@aws-sdk/client-dsql"; async function getCluster(region, clusterId) { const client = new DSQLClient({ region }); const getClusterCommand = new GetClusterCommand({ identifier: clusterId, }); try { return await client.send(getClusterCommand); } catch (error) { if (error.name === "ResourceNotFoundException") { console.log("Cluster ID not found or deleted"); } throw error; } } async function main() { const region = "us-east-1"; const clusterId = "<CLUSTER_ID>"; const response = await getCluster(region, clusterId); console.log("Cluster: ", response); } main();
- Java
-
下列範例可讓您取得單一區域叢集的相關資訊。
package org.example; import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dsql.DsqlClient; import software.amazon.awssdk.services.dsql.model.GetClusterResponse; import software.amazon.awssdk.services.dsql.model.ResourceNotFoundException; public class GetCluster { public static void main(String[] args) { Region region = Region.US_EAST_1; String clusterId = "<your cluster id>"; try ( DsqlClient client = DsqlClient.builder() .region(region) .credentialsProvider(DefaultCredentialsProvider.create()) .build() ) { GetClusterResponse cluster = client.getCluster(r -> r.identifier(clusterId)); System.out.println(cluster); } catch (ResourceNotFoundException e) { System.out.printf("Cluster %s not found in %s%n", clusterId, region); } } }
- Rust
-
下列範例可讓您取得單一區域叢集的相關資訊。
use aws_config::load_defaults; use aws_sdk_dsql::operation::get_cluster::GetClusterOutput; use aws_sdk_dsql::{ Client, Config, config::{BehaviorVersion, Region}, }; /// Create a client. We will use this later for performing operations on the cluster. async fn dsql_client(region: &'static str) -> Client { // Load default SDK configuration let sdk_defaults = load_defaults(BehaviorVersion::latest()).await; // You can set your own credentials by following this guide // https://docs.aws.amazon.com/sdk-for-rust/latest/dg/credproviders.html let credentials = sdk_defaults.credentials_provider().unwrap(); let config = Config::builder() .behavior_version(BehaviorVersion::latest()) .credentials_provider(credentials) .region(Region::new(region)) .build(); Client::from_conf(config) } /// Get a ClusterResource from DSQL cluster identifier pub async fn get_cluster(region: &'static str, identifier: &'static str) -> GetClusterOutput { let client = dsql_client(region).await; client .get_cluster() .identifier(identifier) .send() .await .unwrap() } #[tokio::main(flavor = "current_thread")] pub async fn main() -> anyhow::Result<()> { let region = "us-east-1"; let cluster = get_cluster(region, "<your cluster id>").await; println!("{:#?}", cluster); Ok(()) }
- Ruby
-
下列範例可讓您取得單一區域叢集的相關資訊。
require "aws-sdk-dsql" require "pp" def get_cluster(region, identifier) client = Aws::DSQL::Client.new(region: region) client.get_cluster(identifier: identifier) rescue Aws::Errors::ServiceError => e abort "Unable to retrieve cluster #{identifier} in region #{region}: #{e.message}" end def main region = "us-east-1" cluster_id = "<your cluster id>" cluster = get_cluster(region, cluster_id) pp cluster end main if $PROGRAM_NAME == __FILE__
- .NET
-
下列範例可讓您取得單一區域叢集的相關資訊。
using System; using System.Threading.Tasks; using Amazon; using Amazon.DSQL; using Amazon.DSQL.Model; using Amazon.Runtime.Credentials; namespace DSQLExamples.examples { public class GetCluster { /// <summary> /// Create a client. We will use this later for performing operations on the cluster. /// </summary> private static async Task<AmazonDSQLClient> CreateDSQLClient(RegionEndpoint region) { var awsCredentials = await DefaultAWSCredentialsIdentityResolver.GetCredentialsAsync(); var clientConfig = new AmazonDSQLConfig { RegionEndpoint = region }; return new AmazonDSQLClient(awsCredentials, clientConfig); } /// <summary> /// Get information about a DSQL cluster. /// </summary> public static async Task<GetClusterResponse> Get(RegionEndpoint region, string identifier) { using (var client = await CreateDSQLClient(region)) { var getClusterRequest = new GetClusterRequest { Identifier = identifier }; return await client.GetClusterAsync(getClusterRequest); } } private static async Task Main() { var region = RegionEndpoint.USEast1; var clusterId = "<your cluster id>"; var response = await Get(region, clusterId); Console.WriteLine($"Cluster ARN: {response.Arn}"); } } }
- Golang
-
下列範例可讓您取得單一區域叢集的相關資訊。
package main import ( "context" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "log" "time" "github.com/aws/aws-sdk-go-v2/service/dsql" ) func GetCluster(ctx context.Context, region, identifier string) (clusterStatus *dsql.GetClusterOutput, err error) { cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region)) if err != nil { log.Fatalf("Failed to load AWS configuration: %v", err) } // Initialize the DSQL client client := dsql.NewFromConfig(cfg) input := &dsql.GetClusterInput{ Identifier: aws.String(identifier), } clusterStatus, err = client.GetCluster(context.Background(), input) if err != nil { log.Fatalf("Failed to get cluster: %v", err) } log.Printf("Cluster ARN: %s", *clusterStatus.Arn) return clusterStatus, nil } func main() { ctx, cancel := context.WithTimeout(context.Background(), 6*time.Minute) defer cancel() // Example cluster identifier identifier := "<CLUSTER_ID>" region := "us-east-1" _, err := GetCluster(ctx, region, identifier) if err != nil { log.Fatalf("Failed to get cluster: %v", err) } }
更新叢集
下列範例示範如何使用不同的程式設計語言更新單一區域叢集。
- Python
-
若要更新單一區域叢集,請使用下列範例。
import boto3 def update_cluster(region, cluster_id, deletion_protection_enabled): try: client = boto3.client("dsql", region_name=region) return client.update_cluster(identifier=cluster_id, deletionProtectionEnabled=deletion_protection_enabled) except: print("Unable to update cluster") raise def main(): region = "us-east-1" cluster_id = "<your cluster id>" deletion_protection_enabled = False response = update_cluster(region, cluster_id, deletion_protection_enabled) print(f"Updated {response["arn"]} with deletion_protection_enabled: {deletion_protection_enabled}") if __name__ == "__main__": main()
- C++
-
使用下列範例來更新單一區域叢集。
#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/dsql/DSQLClient.h> #include <aws/dsql/model/UpdateClusterRequest.h> #include <iostream> using namespace Aws; using namespace Aws::DSQL; using namespace Aws::DSQL::Model; /** * Updates a cluster in Amazon Aurora DSQL */ UpdateClusterResult UpdateCluster(const Aws::String& region, const Aws::Map<Aws::String, Aws::String>& updateParams) { // Create client for the specified region DSQL::DSQLClientConfiguration clientConfig; clientConfig.region = region; DSQL::DSQLClient client(clientConfig); // Create update request UpdateClusterRequest updateRequest; updateRequest.SetClientToken(Aws::Utils::UUID::RandomUUID()); // Set identifier (required) if (updateParams.find("identifier") != updateParams.end()) { updateRequest.SetIdentifier(updateParams.at("identifier")); } else { throw std::runtime_error("Cluster identifier is required for update operation"); } // Set deletion protection if specified if (updateParams.find("deletion_protection_enabled") != updateParams.end()) { bool deletionProtection = (updateParams.at("deletion_protection_enabled") == "true"); updateRequest.SetDeletionProtectionEnabled(deletionProtection); } // Execute the update auto updateOutcome = client.UpdateCluster(updateRequest); if (!updateOutcome.IsSuccess()) { std::cerr << "Failed to update cluster: " << updateOutcome.GetError().GetMessage() << std::endl; throw std::runtime_error("Unable to update cluster"); } return updateOutcome.GetResult(); } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { try { // Define region and update parameters Aws::String region = "us-east-1"; Aws::String clusterId = "<your cluster id>"; // Create parameter map Aws::Map<Aws::String, Aws::String> updateParams; updateParams["identifier"] = clusterId; updateParams["deletion_protection_enabled"] = "false"; auto updatedCluster = UpdateCluster(region, updateParams); std::cout << "Updated " << updatedCluster.GetArn() << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } } Aws::ShutdownAPI(options); return 0; }
- JavaScript
-
若要更新單一區域叢集,請使用下列範例。
import { DSQLClient, UpdateClusterCommand } from "@aws-sdk/client-dsql"; export async function updateCluster(region, clusterId, deletionProtectionEnabled) { const client = new DSQLClient({ region }); const updateClusterCommand = new UpdateClusterCommand({ identifier: clusterId, deletionProtectionEnabled: deletionProtectionEnabled }); try { return await client.send(updateClusterCommand); } catch (error) { console.error("Unable to update cluster", error.message); throw error; } } async function main() { const region = "us-east-1"; const clusterId = "<CLUSTER_ID>"; const deletionProtectionEnabled = false; const response = await updateCluster(region, clusterId, deletionProtectionEnabled); console.log(`Updated ${response.arn}`); } main();
- Java
-
使用下列範例來更新單一區域叢集。
package org.example; import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dsql.DsqlClient; import software.amazon.awssdk.services.dsql.model.UpdateClusterRequest; import software.amazon.awssdk.services.dsql.model.UpdateClusterResponse; public class UpdateCluster { public static void main(String[] args) { Region region = Region.US_EAST_1; String clusterId = "<your cluster id>"; try ( DsqlClient client = DsqlClient.builder() .region(region) .credentialsProvider(DefaultCredentialsProvider.create()) .build() ) { UpdateClusterRequest request = UpdateClusterRequest.builder() .identifier(clusterId) .deletionProtectionEnabled(false) .build(); UpdateClusterResponse cluster = client.updateCluster(request); System.out.println("Updated " + cluster.arn()); } } }
- Rust
-
使用下列範例來更新單一區域叢集。
use aws_config::load_defaults; use aws_sdk_dsql::operation::update_cluster::UpdateClusterOutput; use aws_sdk_dsql::{ Client, Config, config::{BehaviorVersion, Region}, }; /// Create a client. We will use this later for performing operations on the cluster. async fn dsql_client(region: &'static str) -> Client { // Load default SDK configuration let sdk_defaults = load_defaults(BehaviorVersion::latest()).await; // You can set your own credentials by following this guide // https://docs.aws.amazon.com/sdk-for-rust/latest/dg/credproviders.html let credentials = sdk_defaults.credentials_provider().unwrap(); let config = Config::builder() .behavior_version(BehaviorVersion::latest()) .credentials_provider(credentials) .region(Region::new(region)) .build(); Client::from_conf(config) } /// Update a DSQL cluster and set delete protection to false. Also add new tags. pub async fn update_cluster(region: &'static str, identifier: &'static str) -> UpdateClusterOutput { let client = dsql_client(region).await; // Update delete protection let update_response = client .update_cluster() .identifier(identifier) .deletion_protection_enabled(false) .send() .await .unwrap(); update_response } #[tokio::main(flavor = "current_thread")] pub async fn main() -> anyhow::Result<()> { let region = "us-east-1"; let cluster = update_cluster(region, "<your cluster id>").await; println!("{:#?}", cluster); Ok(()) }
- Ruby
-
使用下列範例來更新單一區域叢集。
require "aws-sdk-dsql" def update_cluster(region, update_params) client = Aws::DSQL::Client.new(region: region) client.update_cluster(update_params) rescue Aws::Errors::ServiceError => e abort "Unable to update cluster: #{e.message}" end def main region = "us-east-1" cluster_id = "<your cluster id>" updated_cluster = update_cluster(region, { identifier: cluster_id, deletion_protection_enabled: false }) puts "Updated #{updated_cluster.arn}" end main if $PROGRAM_NAME == __FILE__
- .NET
-
使用下列範例來更新單一區域叢集。
using System; using System.Threading.Tasks; using Amazon; using Amazon.DSQL; using Amazon.DSQL.Model; using Amazon.Runtime.Credentials; namespace DSQLExamples.examples { public class UpdateCluster { /// <summary> /// Create a client. We will use this later for performing operations on the cluster. /// </summary> private static async Task<AmazonDSQLClient> CreateDSQLClient(RegionEndpoint region) { var awsCredentials = await DefaultAWSCredentialsIdentityResolver.GetCredentialsAsync(); var clientConfig = new AmazonDSQLConfig { RegionEndpoint = region }; return new AmazonDSQLClient(awsCredentials, clientConfig); } /// <summary> /// Update a DSQL cluster and set delete protection to false. /// </summary> public static async Task<UpdateClusterResponse> Update(RegionEndpoint region, string identifier) { using (var client = await CreateDSQLClient(region)) { var updateClusterRequest = new UpdateClusterRequest { Identifier = identifier, DeletionProtectionEnabled = false }; UpdateClusterResponse response = await client.UpdateClusterAsync(updateClusterRequest); Console.WriteLine($"Updated {response.Arn}"); return response; } } private static async Task Main() { var region = RegionEndpoint.USEast1; var clusterId = "<your cluster id>"; await Update(region, clusterId); } } }
- Golang
-
使用下列範例來更新單一區域叢集。
package main import ( "context" "github.com/aws/aws-sdk-go-v2/config" "log" "time" "github.com/aws/aws-sdk-go-v2/service/dsql" ) func UpdateCluster(ctx context.Context, region, id string, deleteProtection bool) (clusterStatus *dsql.UpdateClusterOutput, err error) { cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region)) if err != nil { log.Fatalf("Failed to load AWS configuration: %v", err) } // Initialize the DSQL client client := dsql.NewFromConfig(cfg) input := dsql.UpdateClusterInput{ Identifier: &id, DeletionProtectionEnabled: &deleteProtection, } clusterStatus, err = client.UpdateCluster(context.Background(), &input) if err != nil { log.Fatalf("Failed to update cluster: %v", err) } log.Printf("Cluster updated successfully: %v", clusterStatus.Status) return clusterStatus, nil } func main() { ctx, cancel := context.WithTimeout(context.Background(), 6*time.Minute) defer cancel() // Example cluster identifier identifier := "<CLUSTER_ID>" region := "us-east-1" deleteProtection := false _, err := UpdateCluster(ctx, region, identifier, deleteProtection) if err != nil { log.Fatalf("Failed to update cluster: %v", err) } }
刪除叢集
下列範例示範如何使用不同的程式設計語言刪除單一區域叢集。
- Python
-
若要刪除單一叢集 AWS 區域,請使用下列範例。
import boto3 def delete_cluster(region, identifier): try: client = boto3.client("dsql", region_name=region) cluster = client.delete_cluster(identifier=identifier) print(f"Initiated delete of {cluster["arn"]}") print("Waiting for cluster to finish deletion") client.get_waiter("cluster_not_exists").wait( identifier=cluster["identifier"], WaiterConfig={ 'Delay': 10, 'MaxAttempts': 30 } ) except: print("Unable to delete cluster " + identifier) raise def main(): region = "us-east-1" cluster_id = "<cluster id>" # Use a placeholder in docs delete_cluster(region, cluster_id) print(f"Deleted {cluster_id}") if __name__ == "__main__": main()
- C++
-
若要刪除單一叢集 AWS 區域,請使用下列範例。
#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/dsql/DSQLClient.h> #include <aws/dsql/model/DeleteClusterRequest.h> #include <aws/dsql/model/GetClusterRequest.h> #include <iostream> #include <thread> #include <chrono> using namespace Aws; using namespace Aws::DSQL; using namespace Aws::DSQL::Model; /** * Deletes a single-region cluster in Amazon Aurora DSQL */ void DeleteCluster(const Aws::String& region, const Aws::String& identifier) { // Create client for the specified region DSQL::DSQLClientConfiguration clientConfig; clientConfig.region = region; DSQL::DSQLClient client(clientConfig); // Delete the cluster DeleteClusterRequest deleteRequest; deleteRequest.SetIdentifier(identifier); deleteRequest.SetClientToken(Aws::Utils::UUID::RandomUUID()); auto deleteOutcome = client.DeleteCluster(deleteRequest); if (!deleteOutcome.IsSuccess()) { std::cerr << "Failed to delete cluster " << identifier << " in " << region << ": " << deleteOutcome.GetError().GetMessage() << std::endl; throw std::runtime_error("Unable to delete cluster " + identifier + " in " + region); } auto cluster = deleteOutcome.GetResult(); std::cout << "Initiated delete of " << cluster.GetArn() << std::endl; } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { try { // Define region and cluster ID Aws::String region = "us-east-1"; Aws::String clusterId = "<your cluster id>"; DeleteCluster(region, clusterId); std::cout << "Deleted " << clusterId << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } } Aws::ShutdownAPI(options); return 0; }
- JavaScript
-
若要刪除單一叢集 AWS 區域,請使用下列範例。
import { DSQLClient, DeleteClusterCommand, waitUntilClusterNotExists } from "@aws-sdk/client-dsql"; async function deleteCluster(region, clusterId) { const client = new DSQLClient({ region }); try { const deleteClusterCommand = new DeleteClusterCommand({ identifier: clusterId, }); const response = await client.send(deleteClusterCommand); console.log(`Waiting for cluster ${response.identifier} to finish deletion`); await waitUntilClusterNotExists( { client: client, maxWaitTime: 300 // Wait for 5 minutes }, { identifier: response.identifier } ); console.log(`Cluster Id ${response.identifier} is now deleted`); return; } catch (error) { if (error.name === "ResourceNotFoundException") { console.log("Cluster ID not found or already deleted"); } else { console.error("Unable to delete cluster: ", error.message); } throw error; } } async function main() { const region = "us-east-1"; const clusterId = "<CLUSTER_ID>"; await deleteCluster(region, clusterId); } main();
- Java
-
若要刪除單一叢集 AWS 區域,請使用下列範例。
package org.example; import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.retries.api.BackoffStrategy; import software.amazon.awssdk.services.dsql.DsqlClient; import software.amazon.awssdk.services.dsql.model.DeleteClusterResponse; import software.amazon.awssdk.services.dsql.model.ResourceNotFoundException; import java.time.Duration; public class DeleteCluster { public static void main(String[] args) { Region region = Region.US_EAST_1; String clusterId = "<your cluster id>"; try ( DsqlClient client = DsqlClient.builder() .region(region) .credentialsProvider(DefaultCredentialsProvider.create()) .build() ) { DeleteClusterResponse cluster = client.deleteCluster(r -> r.identifier(clusterId)); System.out.println("Initiated delete of " + cluster.arn()); // The DSQL SDK offers a built-in waiter to poll for deletion. System.out.println("Waiting for cluster to finish deletion"); client.waiter().waitUntilClusterNotExists( getCluster -> getCluster.identifier(clusterId), config -> config.backoffStrategyV2( BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(10)) ).waitTimeout(Duration.ofMinutes(5)) ); System.out.println("Deleted " + cluster.arn()); } catch (ResourceNotFoundException e) { System.out.printf("Cluster %s not found in %s%n", clusterId, region); } } }
- Rust
-
若要刪除單一叢集 AWS 區域,請使用下列範例。
use aws_config::load_defaults; use aws_sdk_dsql::client::Waiters; use aws_sdk_dsql::{ Client, Config, config::{BehaviorVersion, Region}, }; /// Create a client. We will use this later for performing operations on the cluster. async fn dsql_client(region: &'static str) -> Client { // Load default SDK configuration let sdk_defaults = load_defaults(BehaviorVersion::latest()).await; // You can set your own credentials by following this guide // https://docs.aws.amazon.com/sdk-for-rust/latest/dg/credproviders.html let credentials = sdk_defaults.credentials_provider().unwrap(); let config = Config::builder() .behavior_version(BehaviorVersion::latest()) .credentials_provider(credentials) .region(Region::new(region)) .build(); Client::from_conf(config) } /// Delete a DSQL cluster pub async fn delete_cluster(region: &'static str, identifier: &'static str) { let client = dsql_client(region).await; let delete_response = client .delete_cluster() .identifier(identifier) .send() .await .unwrap(); println!("Initiated delete of {}", delete_response.arn); println!("Waiting for cluster to finish deletion"); client .wait_until_cluster_not_exists() .identifier(identifier) .wait(std::time::Duration::from_secs(300)) // Wait up to 5 minutes .await .unwrap(); } #[tokio::main(flavor = "current_thread")] pub async fn main() -> anyhow::Result<()> { let region = "us-east-1"; let cluster_id = "<cluster to be deleted>"; delete_cluster(region, cluster_id).await; println!("Deleted {cluster_id}"); Ok(()) }
- Ruby
-
若要刪除單一叢集 AWS 區域,請使用下列範例。
require "aws-sdk-dsql" def delete_cluster(region, identifier) client = Aws::DSQL::Client.new(region: region) cluster = client.delete_cluster(identifier: identifier) puts "Initiated delete of #{cluster.arn}" # The DSQL SDK offers built-in waiters to poll for deletion. puts "Waiting for cluster to finish deletion" client.wait_until(:cluster_not_exists, identifier: cluster.identifier) do |w| # Wait for 5 minutes w.max_attempts = 30 w.delay = 10 end rescue Aws::Errors::ServiceError => e abort "Unable to delete cluster #{identifier} in #{region}: #{e.message}" end def main region = "us-east-1" cluster_id = "<your cluster id>" delete_cluster(region, cluster_id) puts "Deleted #{cluster_id}" end main if $PROGRAM_NAME == __FILE__
- .NET
-
若要刪除單一叢集 AWS 區域,請使用下列範例。
using System; using System.Threading.Tasks; using Amazon; using Amazon.DSQL; using Amazon.DSQL.Model; using Amazon.Runtime.Credentials; namespace DSQLExamples.examples { public class DeleteSingleRegionCluster { /// <summary> /// Create a client. We will use this later for performing operations on the cluster. /// </summary> private static async Task<AmazonDSQLClient> CreateDSQLClient(RegionEndpoint region) { var awsCredentials = await DefaultAWSCredentialsIdentityResolver.GetCredentialsAsync(); var clientConfig = new AmazonDSQLConfig { RegionEndpoint = region }; return new AmazonDSQLClient(awsCredentials, clientConfig); } /// <summary> /// Delete a DSQL cluster. /// </summary> public static async Task Delete(RegionEndpoint region, string identifier) { using (var client = await CreateDSQLClient(region)) { var deleteRequest = new DeleteClusterRequest { Identifier = identifier }; var deleteResponse = await client.DeleteClusterAsync(deleteRequest); Console.WriteLine($"Initiated deletion of {deleteResponse.Arn}"); } } private static async Task Main() { var region = RegionEndpoint.USEast1; var clusterId = "<cluster to be deleted>"; await Delete(region, clusterId); } } }
- Golang
-
若要刪除單一叢集 AWS 區域,請使用下列範例。
package main import ( "context" "fmt" "log" "time" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/dsql" ) func DeleteSingleRegion(ctx context.Context, identifier, region string) error { cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region)) if err != nil { log.Fatalf("Failed to load AWS configuration: %v", err) } // Initialize the DSQL client client := dsql.NewFromConfig(cfg) // Create delete cluster input deleteInput := &dsql.DeleteClusterInput{ Identifier: &identifier, } // Delete the cluster result, err := client.DeleteCluster(ctx, deleteInput) if err != nil { return fmt.Errorf("failed to delete cluster: %w", err) } fmt.Printf("Initiated deletion of cluster: %s\n", *result.Arn) // Create waiter to check cluster deletion waiter := dsql.NewClusterNotExistsWaiter(client, func(options *dsql.ClusterNotExistsWaiterOptions) { options.MinDelay = 10 * time.Second options.MaxDelay = 30 * time.Second options.LogWaitAttempts = true }) // Create the input for checking cluster status getInput := &dsql.GetClusterInput{ Identifier: &identifier, } // Wait for the cluster to be deleted fmt.Printf("Waiting for cluster %s to be deleted...\n", identifier) err = waiter.Wait(ctx, getInput, 5*time.Minute) if err != nil { return fmt.Errorf("error waiting for cluster to be deleted: %w", err) } fmt.Printf("Cluster %s has been successfully deleted\n", identifier) return nil } func DeleteCluster(ctx context.Context) { } // Example usage in main function func main() { // Your existing setup code for client configuration... ctx, cancel := context.WithTimeout(context.Background(), 6*time.Minute) defer cancel() // Example cluster identifier // Need to make sure that cluster does not have delete protection enabled identifier := "<CLUSTER_ID>" region := "us-east-1" err := DeleteSingleRegion(ctx, identifier, region) if err != nil { log.Fatalf("Failed to delete cluster: %v", err) } }
如需更多程式碼範例和範例,請造訪 Aurora DSQL 範例 GitHub 儲存庫