Hello Aurora - Amazon Aurora

Hello Aurora

다음 코드 예제에서는 Aurora를 사용하여 시작하는 방법을 보여줍니다.

.NET
AWS SDK for .NET
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

using Amazon.RDS; using Amazon.RDS.Model; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace AuroraActions; public static class HelloAurora { static async Task Main(string[] args) { // Use the AWS .NET Core Setup package to set up dependency injection for the // Amazon Relational Database Service (Amazon RDS). // Use your AWS profile name, or leave it blank to use the default profile. using var host = Host.CreateDefaultBuilder(args) .ConfigureServices((_, services) => services.AddAWSService<IAmazonRDS>() ).Build(); // Now the client is available for injection. Fetching it directly here for example purposes only. var rdsClient = host.Services.GetRequiredService<IAmazonRDS>(); // You can use await and any of the async methods to get a response. var response = await rdsClient.DescribeDBClustersAsync(new DescribeDBClustersRequest { IncludeShared = true }); Console.WriteLine($"Hello Amazon RDS Aurora! Let's list some clusters in this account:"); foreach (var cluster in response.DBClusters) { Console.WriteLine($"\tCluster: database: {cluster.DatabaseName} identifier: {cluster.DBClusterIdentifier}."); } } }
  • API 세부 정보는 AWS SDK for .NET API 참조DescribeDBClusters를 참조하십시오.

C++
SDK for C++
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

CMakeLists.txt CMake 파일의 코드입니다.

# Set the minimum required version of CMake for this project. cmake_minimum_required(VERSION 3.13) # Set the AWS service components used by this project. set(SERVICE_COMPONENTS rds) # Set this project's name. project("hello_aurora") # Set the C++ standard to use to build this target. # At least C++ 11 is required for the AWS SDK for C++. set(CMAKE_CXX_STANDARD 11) # Use the MSVC variable to determine if this is a Windows build. set(WINDOWS_BUILD ${MSVC}) if (WINDOWS_BUILD) # Set the location where CMake can find the installed libraries for the AWS SDK. string(REPLACE ";" "/aws-cpp-sdk-all;" SYSTEM_MODULE_PATH "${CMAKE_SYSTEM_PREFIX_PATH}/aws-cpp-sdk-all") list(APPEND CMAKE_PREFIX_PATH ${SYSTEM_MODULE_PATH}) endif () # Find the AWS SDK for C++ package. find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS}) if (WINDOWS_BUILD AND AWSSDK_INSTALL_AS_SHARED_LIBS) # Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging. # set(BIN_SUB_DIR "/Debug") # If you are building from the command line, you may need to uncomment this # and set the proper subdirectory to the executables' location. AWSSDK_CPY_DYN_LIBS(SERVICE_COMPONENTS "" ${CMAKE_CURRENT_BINARY_DIR}${BIN_SUB_DIR}) endif () add_executable(${PROJECT_NAME} hello_aurora.cpp) target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})

hello_aurora.cpp 소스 파일의 코드입니다.

#include <aws/core/Aws.h> #include <aws/rds/RDSClient.h> #include <aws/rds/model/DescribeDBClustersRequest.h> #include <iostream> /* * A "Hello Aurora" starter application which initializes an Amazon Relational Database Service (Amazon RDS) client * and describes the Amazon Aurora (Aurora) clusters. * * main function * * Usage: 'hello_aurora' * */ int main(int argc, char **argv) { Aws::SDKOptions options; // Optionally change the log level for debugging. // options.loggingOptions.logLevel = Utils::Logging::LogLevel::Debug; Aws::InitAPI(options); // Should only be called once. int result = 0; { Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient rdsClient(clientConfig); Aws::String marker; // Used for pagination. std::vector<Aws::String> clusterIds; do { Aws::RDS::Model::DescribeDBClustersRequest request; Aws::RDS::Model::DescribeDBClustersOutcome outcome = rdsClient.DescribeDBClusters(request); if (outcome.IsSuccess()) { for (auto &cluster: outcome.GetResult().GetDBClusters()) { clusterIds.push_back(cluster.GetDBClusterIdentifier()); } marker = outcome.GetResult().GetMarker(); } else { result = 1; std::cerr << "Error with Aurora::GDescribeDBClusters. " << outcome.GetError().GetMessage() << std::endl; break; } } while (!marker.empty()); std::cout << clusterIds.size() << " Aurora clusters found." << std::endl; for (auto &clusterId: clusterIds) { std::cout << " clusterId " << clusterId << std::endl; } } Aws::ShutdownAPI(options); // Should only be called once. return 0; }
  • API 세부 정보는 AWS SDK for C++ API 참조DescribeDBClusters를 참조하십시오.

Go
SDK for Go V2
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

package main import ( "context" "fmt" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/rds" ) // main uses the AWS SDK for Go V2 to create an Amazon Aurora client and list up to 20 // DB clusters in your account. // This example uses the default settings specified in your shared credentials // and config files. func main() { ctx := context.Background() sdkConfig, err := config.LoadDefaultConfig(ctx) if err != nil { fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") fmt.Println(err) return } auroraClient := rds.NewFromConfig(sdkConfig) const maxClusters = 20 fmt.Printf("Let's list up to %v DB clusters.\n", maxClusters) output, err := auroraClient.DescribeDBClusters( ctx, &rds.DescribeDBClustersInput{MaxRecords: aws.Int32(maxClusters)}) if err != nil { fmt.Printf("Couldn't list DB clusters: %v\n", err) return } if len(output.DBClusters) == 0 { fmt.Println("No DB clusters found.") } else { for _, cluster := range output.DBClusters { fmt.Printf("DB cluster %v has database %v.\n", *cluster.DBClusterIdentifier, *cluster.DatabaseName) } } }
  • API 세부 정보는 AWS SDK for Go API 참조DescribeDBClusters를 참조하십시오.

Java
SDK for Java 2.x
참고

GitHub에 더 많은 내용이 있습니다. AWS코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rds.RdsClient; import software.amazon.awssdk.services.rds.paginators.DescribeDBClustersIterable; public class DescribeDbClusters { public static void main(String[] args) { Region region = Region.US_EAST_1; RdsClient rdsClient = RdsClient.builder() .region(region) .build(); describeClusters(rdsClient); rdsClient.close(); } public static void describeClusters(RdsClient rdsClient) { DescribeDBClustersIterable clustersIterable = rdsClient.describeDBClustersPaginator(); clustersIterable.stream() .flatMap(r -> r.dbClusters().stream()) .forEach(cluster -> System.out .println("Database name: " + cluster.databaseName() + " Arn = " + cluster.dbClusterArn())); } }
  • API 세부 정보는 AWS SDK for Java 2.x API 참조DescribeDBClusters를 참조하십시오.

Python
SDK for Python (Boto3)
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

import boto3 # Create an RDS client rds = boto3.client("rds") # Create a paginator for the describe_db_clusters operation paginator = rds.get_paginator("describe_db_clusters") # Use the paginator to get a list of DB clusters response_iterator = paginator.paginate( PaginationConfig={ "PageSize": 50, # Adjust PageSize as needed "StartingToken": None, } ) # Iterate through the pages of the response clusters_found = False for page in response_iterator: if "DBClusters" in page and page["DBClusters"]: clusters_found = True print("Here are your RDS Aurora clusters:") for cluster in page["DBClusters"]: print( f"Cluster ID: {cluster['DBClusterIdentifier']}, Engine: {cluster['Engine']}" ) if not clusters_found: print("No clusters found!")
  • API 세부 정보는 AWS SDK for Python (Boto3) API 참조DescribeDBClusters를 참조하십시오.

Ruby
SDK for Ruby
참고

GitHub에 더 많은 내용이 있습니다. AWS코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

require 'aws-sdk-rds' # Creates an Amazon RDS client for the AWS Region rds = Aws::RDS::Client.new puts 'Listing clusters in this AWS account...' # Calls the describe_db_clusters method to get information about clusters resp = rds.describe_db_clusters(max_records: 20) # Checks if any clusters are found and prints the appropriate message if resp.db_clusters.empty? puts 'No clusters found!' else # Loops through the array of cluster objects and prints the cluster identifier resp.db_clusters.each do |cluster| puts "Cluster identifier: #{cluster.db_cluster_identifier}" end end
  • API 세부 정보는 AWS SDK for Ruby API 참조DescribeDBClusters를 참조하십시오.

Rust
SDK for Rust
참고

GitHub에 더 많은 내용이 있습니다. AWS코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

use aws_sdk_rds::Client; #[derive(Debug)] struct Error(String); impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } impl std::error::Error for Error {} #[tokio::main] async fn main() -> Result<(), Error> { tracing_subscriber::fmt::init(); let sdk_config = aws_config::from_env().load().await; let client = Client::new(&sdk_config); let describe_db_clusters_output = client .describe_db_clusters() .send() .await .map_err(|e| Error(e.to_string()))?; println!( "Found {} clusters:", describe_db_clusters_output.db_clusters().len() ); for cluster in describe_db_clusters_output.db_clusters() { let name = cluster.database_name().unwrap_or("Unknown"); let engine = cluster.engine().unwrap_or("Unknown"); let id = cluster.db_cluster_identifier().unwrap_or("Unknown"); let class = cluster.db_cluster_instance_class().unwrap_or("Unknown"); println!("\tDatabase: {name}",); println!("\t Engine: {engine}",); println!("\t ID: {id}",); println!("\tInstance: {class}",); } Ok(()) }
  • API 세부 정보는 AWS SDK for Rust API 참조의 DescribeDBClusters를 참조하세요.

AWS SDK 개발자 가이드 및 코드 예시의 전체 목록은 AWS SDK와 함께 이 서비스 사용 단원을 참조하세요. 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.