AWS Doc SDK ExamplesWord
기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
Java 2.x용 SDK를 사용한 Aurora 예제
다음 코드 예제에서는 Aurora와 AWS SDK for Java 2.x 함께를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.
기본 사항은 서비스 내에서 필수 작업을 수행하는 방법을 보여주는 코드 예제입니다.
작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접적으로 호출하는 방법을 보여주며 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.
시나리오는 동일한 서비스 내에서 또는 다른 AWS 서비스와 결합된 상태에서 여러 함수를 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예제입니다.
각 예제에는 컨텍스트에서 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있는 전체 소스 코드에 대한 링크가 포함되어 있습니다.
시작
다음 코드 예제에서는 Aurora를 사용하여 시작하는 방법을 보여줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on 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를 참조하세요.
-
기본 사항
다음 코드 예시는 다음과 같은 작업을 수행하는 방법을 보여줍니다.
사용자 지정 Aurora DB 클러스터 파라미터 그룹을 만들고 파라미터 값을 설정합니다.
파라미터 그룹을 사용하는 DB 클러스터를 생성합니다.
데이터베이스가 포함된 DB 인스턴스를 생성합니다.
DB 클러스터의 스냅샷을 만든 다음, 리소스를 정리합니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. /** * Before running this Java (v2) code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html * * This example requires an AWS Secrets Manager secret that contains the * database credentials. If you do not create a * secret, this example will not work. For details, see: * * https://docs.aws.amazon.com/secretsmanager/latest/userguide/integrating_how-services-use-secrets_RS.html * * This Java example performs the following tasks: * * 1. Gets available engine families for Amazon Aurora MySQL-Compatible Edition * by calling the DescribeDbEngineVersions(Engine='aurora-mysql') method. * 2. Selects an engine family and creates a custom DB cluster parameter group * by invoking the describeDBClusterParameters method. * 3. Gets the parameter groups by invoking the describeDBClusterParameterGroups * method. * 4. Gets parameters in the group by invoking the describeDBClusterParameters * method. * 5. Modifies the auto_increment_offset parameter by invoking the * modifyDbClusterParameterGroupRequest method. * 6. Gets and displays the updated parameters. * 7. Gets a list of allowed engine versions by invoking the * describeDbEngineVersions method. * 8. Creates an Aurora DB cluster database cluster that contains a MySQL * database. * 9. Waits for DB instance to be ready. * 10. Gets a list of instance classes available for the selected engine. * 11. Creates a database instance in the cluster. * 12. Waits for DB instance to be ready. * 13. Creates a snapshot. * 14. Waits for DB snapshot to be ready. * 15. Deletes the DB cluster. * 16. Deletes the DB cluster group. */ public class AuroraScenario { public static long sleepTime = 20; public static final String DASHES = new String(new char[80]).replace("\0", "-"); public static void main(String[] args) throws InterruptedException { final String usage = "\n" + "Usage:\n" + " <dbClusterGroupName> <dbParameterGroupFamily> <dbInstanceClusterIdentifier> <dbInstanceIdentifier> <dbName> <dbSnapshotIdentifier><secretName>" + "Where:\n" + " dbClusterGroupName - The name of the DB cluster parameter group. \n" + " dbParameterGroupFamily - The DB cluster parameter group family name (for example, aurora-mysql5.7). \n" + " dbInstanceClusterIdentifier - The instance cluster identifier value.\n" + " dbInstanceIdentifier - The database instance identifier.\n" + " dbName - The database name.\n" + " dbSnapshotIdentifier - The snapshot identifier.\n" + " secretName - The name of the AWS Secrets Manager secret that contains the database credentials\"\n"; ; if (args.length != 7) { System.out.println(usage); System.exit(1); } String dbClusterGroupName = args[0]; String dbParameterGroupFamily = args[1]; String dbInstanceClusterIdentifier = args[2]; String dbInstanceIdentifier = args[3]; String dbName = args[4]; String dbSnapshotIdentifier = args[5]; String secretName = args[6]; // Retrieve the database credentials using AWS Secrets Manager. Gson gson = new Gson(); User user = gson.fromJson(String.valueOf(getSecretValues(secretName)), User.class); String username = user.getUsername(); String userPassword = user.getPassword(); Region region = Region.US_WEST_2; RdsClient rdsClient = RdsClient.builder() .region(region) .build(); System.out.println(DASHES); System.out.println("Welcome to the Amazon Aurora example scenario."); System.out.println(DASHES); System.out.println(DASHES); System.out.println("1. Return a list of the available DB engines"); describeDBEngines(rdsClient); System.out.println(DASHES); System.out.println(DASHES); System.out.println("2. Create a custom parameter group"); createDBClusterParameterGroup(rdsClient, dbClusterGroupName, dbParameterGroupFamily); System.out.println(DASHES); System.out.println(DASHES); System.out.println("3. Get the parameter group"); describeDbClusterParameterGroups(rdsClient, dbClusterGroupName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("4. Get the parameters in the group"); describeDbClusterParameters(rdsClient, dbClusterGroupName, 0); System.out.println(DASHES); System.out.println(DASHES); System.out.println("5. Modify the auto_increment_offset parameter"); modifyDBClusterParas(rdsClient, dbClusterGroupName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("6. Display the updated parameter value"); describeDbClusterParameters(rdsClient, dbClusterGroupName, -1); System.out.println(DASHES); System.out.println(DASHES); System.out.println("7. Get a list of allowed engine versions"); getAllowedEngines(rdsClient, dbParameterGroupFamily); System.out.println(DASHES); System.out.println(DASHES); System.out.println("8. Create an Aurora DB cluster database"); String arnClusterVal = createDBCluster(rdsClient, dbClusterGroupName, dbName, dbInstanceClusterIdentifier, username, userPassword); System.out.println("The ARN of the cluster is " + arnClusterVal); System.out.println(DASHES); System.out.println(DASHES); System.out.println("9. Wait for DB instance to be ready"); waitForInstanceReady(rdsClient, dbInstanceClusterIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("10. Get a list of instance classes available for the selected engine"); String instanceClass = getListInstanceClasses(rdsClient); System.out.println(DASHES); System.out.println(DASHES); System.out.println("11. Create a database instance in the cluster."); String clusterDBARN = createDBInstanceCluster(rdsClient, dbInstanceIdentifier, dbInstanceClusterIdentifier, instanceClass); System.out.println("The ARN of the database is " + clusterDBARN); System.out.println(DASHES); System.out.println(DASHES); System.out.println("12. Wait for DB instance to be ready"); waitDBInstanceReady(rdsClient, dbInstanceIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("13. Create a snapshot"); createDBClusterSnapshot(rdsClient, dbInstanceClusterIdentifier, dbSnapshotIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("14. Wait for DB snapshot to be ready"); waitForSnapshotReady(rdsClient, dbSnapshotIdentifier, dbInstanceClusterIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("14. Delete the DB instance"); deleteDatabaseInstance(rdsClient, dbInstanceIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("15. Delete the DB cluster"); deleteCluster(rdsClient, dbInstanceClusterIdentifier); System.out.println(DASHES); System.out.println(DASHES); System.out.println("16. Delete the DB cluster group"); deleteDBClusterGroup(rdsClient, dbClusterGroupName, clusterDBARN); System.out.println(DASHES); System.out.println(DASHES); System.out.println("The Scenario has successfully completed."); System.out.println(DASHES); rdsClient.close(); } private static SecretsManagerClient getSecretClient() { Region region = Region.US_WEST_2; return SecretsManagerClient.builder() .region(region) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); } private static String getSecretValues(String secretName) { SecretsManagerClient secretClient = getSecretClient(); GetSecretValueRequest valueRequest = GetSecretValueRequest.builder() .secretId(secretName) .build(); GetSecretValueResponse valueResponse = secretClient.getSecretValue(valueRequest); return valueResponse.secretString(); } public static void deleteDBClusterGroup(RdsClient rdsClient, String dbClusterGroupName, String clusterDBARN) throws InterruptedException { try { boolean isDataDel = false; boolean didFind; String instanceARN; // Make sure that the database has been deleted. while (!isDataDel) { DescribeDbInstancesResponse response = rdsClient.describeDBInstances(); List<DBInstance> instanceList = response.dbInstances(); int listSize = instanceList.size(); didFind = false; int index = 1; for (DBInstance instance : instanceList) { instanceARN = instance.dbInstanceArn(); if (instanceARN.compareTo(clusterDBARN) == 0) { System.out.println(clusterDBARN + " still exists"); didFind = true; } if ((index == listSize) && (!didFind)) { // Went through the entire list and did not find the database ARN. isDataDel = true; } Thread.sleep(sleepTime * 1000); index++; } } DeleteDbClusterParameterGroupRequest clusterParameterGroupRequest = DeleteDbClusterParameterGroupRequest .builder() .dbClusterParameterGroupName(dbClusterGroupName) .build(); rdsClient.deleteDBClusterParameterGroup(clusterParameterGroupRequest); System.out.println(dbClusterGroupName + " was deleted."); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void deleteCluster(RdsClient rdsClient, String dbInstanceClusterIdentifier) { try { DeleteDbClusterRequest deleteDbClusterRequest = DeleteDbClusterRequest.builder() .dbClusterIdentifier(dbInstanceClusterIdentifier) .skipFinalSnapshot(true) .build(); rdsClient.deleteDBCluster(deleteDbClusterRequest); System.out.println(dbInstanceClusterIdentifier + " was deleted!"); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void deleteDatabaseInstance(RdsClient rdsClient, String dbInstanceIdentifier) { try { DeleteDbInstanceRequest deleteDbInstanceRequest = DeleteDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .deleteAutomatedBackups(true) .skipFinalSnapshot(true) .build(); DeleteDbInstanceResponse response = rdsClient.deleteDBInstance(deleteDbInstanceRequest); System.out.println("The status of the database is " + response.dbInstance().dbInstanceStatus()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void waitForSnapshotReady(RdsClient rdsClient, String dbSnapshotIdentifier, String dbInstanceClusterIdentifier) { try { boolean snapshotReady = false; String snapshotReadyStr; System.out.println("Waiting for the snapshot to become available."); DescribeDbClusterSnapshotsRequest snapshotsRequest = DescribeDbClusterSnapshotsRequest.builder() .dbClusterSnapshotIdentifier(dbSnapshotIdentifier) .dbClusterIdentifier(dbInstanceClusterIdentifier) .build(); while (!snapshotReady) { DescribeDbClusterSnapshotsResponse response = rdsClient.describeDBClusterSnapshots(snapshotsRequest); List<DBClusterSnapshot> snapshotList = response.dbClusterSnapshots(); for (DBClusterSnapshot snapshot : snapshotList) { snapshotReadyStr = snapshot.status(); if (snapshotReadyStr.contains("available")) { snapshotReady = true; } else { System.out.println("."); Thread.sleep(sleepTime * 5000); } } } System.out.println("The Snapshot is available!"); } catch (RdsException | InterruptedException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void createDBClusterSnapshot(RdsClient rdsClient, String dbInstanceClusterIdentifier, String dbSnapshotIdentifier) { try { CreateDbClusterSnapshotRequest snapshotRequest = CreateDbClusterSnapshotRequest.builder() .dbClusterIdentifier(dbInstanceClusterIdentifier) .dbClusterSnapshotIdentifier(dbSnapshotIdentifier) .build(); CreateDbClusterSnapshotResponse response = rdsClient.createDBClusterSnapshot(snapshotRequest); System.out.println("The Snapshot ARN is " + response.dbClusterSnapshot().dbClusterSnapshotArn()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void waitDBInstanceReady(RdsClient rdsClient, String dbInstanceIdentifier) { boolean instanceReady = false; String instanceReadyStr; System.out.println("Waiting for instance to become available."); try { DescribeDbInstancesRequest instanceRequest = DescribeDbInstancesRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .build(); String endpoint = ""; while (!instanceReady) { DescribeDbInstancesResponse response = rdsClient.describeDBInstances(instanceRequest); List<DBInstance> instanceList = response.dbInstances(); for (DBInstance instance : instanceList) { instanceReadyStr = instance.dbInstanceStatus(); if (instanceReadyStr.contains("available")) { endpoint = instance.endpoint().address(); instanceReady = true; } else { System.out.print("."); Thread.sleep(sleepTime * 1000); } } } System.out.println("Database instance is available! The connection endpoint is " + endpoint); } catch (RdsException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } } public static String createDBInstanceCluster(RdsClient rdsClient, String dbInstanceIdentifier, String dbInstanceClusterIdentifier, String instanceClass) { try { CreateDbInstanceRequest instanceRequest = CreateDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .dbClusterIdentifier(dbInstanceClusterIdentifier) .engine("aurora-mysql") .dbInstanceClass(instanceClass) .build(); CreateDbInstanceResponse response = rdsClient.createDBInstance(instanceRequest); System.out.print("The status is " + response.dbInstance().dbInstanceStatus()); return response.dbInstance().dbInstanceArn(); } catch (RdsException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } public static String getListInstanceClasses(RdsClient rdsClient) { try { DescribeOrderableDbInstanceOptionsRequest optionsRequest = DescribeOrderableDbInstanceOptionsRequest .builder() .engine("aurora-mysql") .maxRecords(20) .build(); DescribeOrderableDbInstanceOptionsResponse response = rdsClient .describeOrderableDBInstanceOptions(optionsRequest); List<OrderableDBInstanceOption> instanceOptions = response.orderableDBInstanceOptions(); String instanceClass = ""; for (OrderableDBInstanceOption instanceOption : instanceOptions) { instanceClass = instanceOption.dbInstanceClass(); System.out.println("The instance class is " + instanceOption.dbInstanceClass()); System.out.println("The engine version is " + instanceOption.engineVersion()); } return instanceClass; } catch (RdsException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } // Waits until the database instance is available. public static void waitForInstanceReady(RdsClient rdsClient, String dbClusterIdentifier) { boolean instanceReady = false; String instanceReadyStr; System.out.println("Waiting for instance to become available."); try { DescribeDbClustersRequest instanceRequest = DescribeDbClustersRequest.builder() .dbClusterIdentifier(dbClusterIdentifier) .build(); while (!instanceReady) { DescribeDbClustersResponse response = rdsClient.describeDBClusters(instanceRequest); List<DBCluster> clusterList = response.dbClusters(); for (DBCluster cluster : clusterList) { instanceReadyStr = cluster.status(); if (instanceReadyStr.contains("available")) { instanceReady = true; } else { System.out.print("."); Thread.sleep(sleepTime * 1000); } } } System.out.println("Database cluster is available!"); } catch (RdsException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } } public static String createDBCluster(RdsClient rdsClient, String dbParameterGroupFamily, String dbName, String dbClusterIdentifier, String userName, String password) { try { CreateDbClusterRequest clusterRequest = CreateDbClusterRequest.builder() .databaseName(dbName) .dbClusterIdentifier(dbClusterIdentifier) .dbClusterParameterGroupName(dbParameterGroupFamily) .engine("aurora-mysql") .masterUsername(userName) .masterUserPassword(password) .build(); CreateDbClusterResponse response = rdsClient.createDBCluster(clusterRequest); return response.dbCluster().dbClusterArn(); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } return ""; } // Get a list of allowed engine versions. public static void getAllowedEngines(RdsClient rdsClient, String dbParameterGroupFamily) { try { DescribeDbEngineVersionsRequest versionsRequest = DescribeDbEngineVersionsRequest.builder() .dbParameterGroupFamily(dbParameterGroupFamily) .engine("aurora-mysql") .build(); DescribeDbEngineVersionsResponse response = rdsClient.describeDBEngineVersions(versionsRequest); List<DBEngineVersion> dbEngines = response.dbEngineVersions(); for (DBEngineVersion dbEngine : dbEngines) { System.out.println("The engine version is " + dbEngine.engineVersion()); System.out.println("The engine description is " + dbEngine.dbEngineDescription()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } // Modify the auto_increment_offset parameter. public static void modifyDBClusterParas(RdsClient rdsClient, String dClusterGroupName) { try { Parameter parameter1 = Parameter.builder() .parameterName("auto_increment_offset") .applyMethod("immediate") .parameterValue("5") .build(); List<Parameter> paraList = new ArrayList<>(); paraList.add(parameter1); ModifyDbClusterParameterGroupRequest groupRequest = ModifyDbClusterParameterGroupRequest.builder() .dbClusterParameterGroupName(dClusterGroupName) .parameters(paraList) .build(); ModifyDbClusterParameterGroupResponse response = rdsClient.modifyDBClusterParameterGroup(groupRequest); System.out.println( "The parameter group " + response.dbClusterParameterGroupName() + " was successfully modified"); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void describeDbClusterParameters(RdsClient rdsClient, String dbCLusterGroupName, int flag) { try { DescribeDbClusterParametersRequest dbParameterGroupsRequest; if (flag == 0) { dbParameterGroupsRequest = DescribeDbClusterParametersRequest.builder() .dbClusterParameterGroupName(dbCLusterGroupName) .build(); } else { dbParameterGroupsRequest = DescribeDbClusterParametersRequest.builder() .dbClusterParameterGroupName(dbCLusterGroupName) .source("user") .build(); } DescribeDbClusterParametersResponse response = rdsClient .describeDBClusterParameters(dbParameterGroupsRequest); List<Parameter> dbParameters = response.parameters(); String paraName; for (Parameter para : dbParameters) { // Only print out information about either auto_increment_offset or // auto_increment_increment. paraName = para.parameterName(); if ((paraName.compareTo("auto_increment_offset") == 0) || (paraName.compareTo("auto_increment_increment ") == 0)) { System.out.println("*** The parameter name is " + paraName); System.out.println("*** The parameter value is " + para.parameterValue()); System.out.println("*** The parameter data type is " + para.dataType()); System.out.println("*** The parameter description is " + para.description()); System.out.println("*** The parameter allowed values is " + para.allowedValues()); } } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void describeDbClusterParameterGroups(RdsClient rdsClient, String dbClusterGroupName) { try { DescribeDbClusterParameterGroupsRequest groupsRequest = DescribeDbClusterParameterGroupsRequest.builder() .dbClusterParameterGroupName(dbClusterGroupName) .maxRecords(20) .build(); List<DBClusterParameterGroup> groups = rdsClient.describeDBClusterParameterGroups(groupsRequest) .dbClusterParameterGroups(); for (DBClusterParameterGroup group : groups) { System.out.println("The group name is " + group.dbClusterParameterGroupName()); System.out.println("The group ARN is " + group.dbClusterParameterGroupArn()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void createDBClusterParameterGroup(RdsClient rdsClient, String dbClusterGroupName, String dbParameterGroupFamily) { try { CreateDbClusterParameterGroupRequest groupRequest = CreateDbClusterParameterGroupRequest.builder() .dbClusterParameterGroupName(dbClusterGroupName) .dbParameterGroupFamily(dbParameterGroupFamily) .description("Created by using the AWS SDK for Java") .build(); CreateDbClusterParameterGroupResponse response = rdsClient.createDBClusterParameterGroup(groupRequest); System.out.println("The group name is " + response.dbClusterParameterGroup().dbClusterParameterGroupName()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } public static void describeDBEngines(RdsClient rdsClient) { try { DescribeDbEngineVersionsRequest engineVersionsRequest = DescribeDbEngineVersionsRequest.builder() .engine("aurora-mysql") .defaultOnly(true) .maxRecords(20) .build(); DescribeDbEngineVersionsResponse response = rdsClient.describeDBEngineVersions(engineVersionsRequest); List<DBEngineVersion> engines = response.dbEngineVersions(); // Get all DBEngineVersion objects. for (DBEngineVersion engineOb : engines) { System.out.println("The name of the DB parameter group family for the database engine is " + engineOb.dbParameterGroupFamily()); System.out.println("The name of the database engine " + engineOb.engine()); System.out.println("The version number of the database engine " + engineOb.engineVersion()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 다음 주제를 참조하세요.
-
작업
다음 코드 예시에서는 CreateDBCluster
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static String createDBCluster(RdsClient rdsClient, String dbParameterGroupFamily, String dbName, String dbClusterIdentifier, String userName, String password) { try { CreateDbClusterRequest clusterRequest = CreateDbClusterRequest.builder() .databaseName(dbName) .dbClusterIdentifier(dbClusterIdentifier) .dbClusterParameterGroupName(dbParameterGroupFamily) .engine("aurora-mysql") .masterUsername(userName) .masterUserPassword(password) .build(); CreateDbClusterResponse response = rdsClient.createDBCluster(clusterRequest); return response.dbCluster().dbClusterArn(); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } return ""; }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 CreateDBCluster를 참조하세요.
-
다음 코드 예시에서는 CreateDBClusterParameterGroup
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void createDBClusterParameterGroup(RdsClient rdsClient, String dbClusterGroupName, String dbParameterGroupFamily) { try { CreateDbClusterParameterGroupRequest groupRequest = CreateDbClusterParameterGroupRequest.builder() .dbClusterParameterGroupName(dbClusterGroupName) .dbParameterGroupFamily(dbParameterGroupFamily) .description("Created by using the AWS SDK for Java") .build(); CreateDbClusterParameterGroupResponse response = rdsClient.createDBClusterParameterGroup(groupRequest); System.out.println("The group name is " + response.dbClusterParameterGroup().dbClusterParameterGroupName()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 CreateDBClusterParameterGroupWord를 참조하세요.
-
다음 코드 예시에서는 CreateDBClusterSnapshot
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void createDBClusterSnapshot(RdsClient rdsClient, String dbInstanceClusterIdentifier, String dbSnapshotIdentifier) { try { CreateDbClusterSnapshotRequest snapshotRequest = CreateDbClusterSnapshotRequest.builder() .dbClusterIdentifier(dbInstanceClusterIdentifier) .dbClusterSnapshotIdentifier(dbSnapshotIdentifier) .build(); CreateDbClusterSnapshotResponse response = rdsClient.createDBClusterSnapshot(snapshotRequest); System.out.println("The Snapshot ARN is " + response.dbClusterSnapshot().dbClusterSnapshotArn()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 CreateDBCluster 스냅샷을 참조하세요.
-
다음 코드 예시에서는 CreateDBInstance
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static String createDBInstanceCluster(RdsClient rdsClient, String dbInstanceIdentifier, String dbInstanceClusterIdentifier, String instanceClass) { try { CreateDbInstanceRequest instanceRequest = CreateDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .dbClusterIdentifier(dbInstanceClusterIdentifier) .engine("aurora-mysql") .dbInstanceClass(instanceClass) .build(); CreateDbInstanceResponse response = rdsClient.createDBInstance(instanceRequest); System.out.print("The status is " + response.dbInstance().dbInstanceStatus()); return response.dbInstance().dbInstanceArn(); } catch (RdsException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 CreateDBInstance를 참조하세요.
-
다음 코드 예시에서는 DeleteDBCluster
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void deleteCluster(RdsClient rdsClient, String dbInstanceClusterIdentifier) { try { DeleteDbClusterRequest deleteDbClusterRequest = DeleteDbClusterRequest.builder() .dbClusterIdentifier(dbInstanceClusterIdentifier) .skipFinalSnapshot(true) .build(); rdsClient.deleteDBCluster(deleteDbClusterRequest); System.out.println(dbInstanceClusterIdentifier + " was deleted!"); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 DeleteDBCluster를 참조하세요.
-
다음 코드 예시에서는 DeleteDBClusterParameterGroup
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void deleteDBClusterGroup(RdsClient rdsClient, String dbClusterGroupName, String clusterDBARN) throws InterruptedException { try { boolean isDataDel = false; boolean didFind; String instanceARN; // Make sure that the database has been deleted. while (!isDataDel) { DescribeDbInstancesResponse response = rdsClient.describeDBInstances(); List<DBInstance> instanceList = response.dbInstances(); int listSize = instanceList.size(); didFind = false; int index = 1; for (DBInstance instance : instanceList) { instanceARN = instance.dbInstanceArn(); if (instanceARN.compareTo(clusterDBARN) == 0) { System.out.println(clusterDBARN + " still exists"); didFind = true; } if ((index == listSize) && (!didFind)) { // Went through the entire list and did not find the database ARN. isDataDel = true; } Thread.sleep(sleepTime * 1000); index++; } } DeleteDbClusterParameterGroupRequest clusterParameterGroupRequest = DeleteDbClusterParameterGroupRequest .builder() .dbClusterParameterGroupName(dbClusterGroupName) .build(); rdsClient.deleteDBClusterParameterGroup(clusterParameterGroupRequest); System.out.println(dbClusterGroupName + " was deleted."); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 DeleteDBClusterParameterGroupWord를 참조하세요.
-
다음 코드 예시에서는 DeleteDBInstance
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void deleteDatabaseInstance(RdsClient rdsClient, String dbInstanceIdentifier) { try { DeleteDbInstanceRequest deleteDbInstanceRequest = DeleteDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .deleteAutomatedBackups(true) .skipFinalSnapshot(true) .build(); DeleteDbInstanceResponse response = rdsClient.deleteDBInstance(deleteDbInstanceRequest); System.out.println("The status of the database is " + response.dbInstance().dbInstanceStatus()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 DeleteDBInstance를 참조하세요.
-
다음 코드 예시에서는 DescribeDBClusterParameterGroups
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void describeDbClusterParameterGroups(RdsClient rdsClient, String dbClusterGroupName) { try { DescribeDbClusterParameterGroupsRequest groupsRequest = DescribeDbClusterParameterGroupsRequest.builder() .dbClusterParameterGroupName(dbClusterGroupName) .maxRecords(20) .build(); List<DBClusterParameterGroup> groups = rdsClient.describeDBClusterParameterGroups(groupsRequest) .dbClusterParameterGroups(); for (DBClusterParameterGroup group : groups) { System.out.println("The group name is " + group.dbClusterParameterGroupName()); System.out.println("The group ARN is " + group.dbClusterParameterGroupArn()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 DescribeDBClusterParameterGroupsWord를 참조하세요.
-
다음 코드 예시에서는 DescribeDBClusterParameters
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void describeDbClusterParameters(RdsClient rdsClient, String dbCLusterGroupName, int flag) { try { DescribeDbClusterParametersRequest dbParameterGroupsRequest; if (flag == 0) { dbParameterGroupsRequest = DescribeDbClusterParametersRequest.builder() .dbClusterParameterGroupName(dbCLusterGroupName) .build(); } else { dbParameterGroupsRequest = DescribeDbClusterParametersRequest.builder() .dbClusterParameterGroupName(dbCLusterGroupName) .source("user") .build(); } DescribeDbClusterParametersResponse response = rdsClient .describeDBClusterParameters(dbParameterGroupsRequest); List<Parameter> dbParameters = response.parameters(); String paraName; for (Parameter para : dbParameters) { // Only print out information about either auto_increment_offset or // auto_increment_increment. paraName = para.parameterName(); if ((paraName.compareTo("auto_increment_offset") == 0) || (paraName.compareTo("auto_increment_increment ") == 0)) { System.out.println("*** The parameter name is " + paraName); System.out.println("*** The parameter value is " + para.parameterValue()); System.out.println("*** The parameter data type is " + para.dataType()); System.out.println("*** The parameter description is " + para.description()); System.out.println("*** The parameter allowed values is " + para.allowedValues()); } } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 DescribeDBCluster 파라미터를 참조하세요.
-
다음 코드 예시에서는 DescribeDBClusterSnapshots
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void waitForSnapshotReady(RdsClient rdsClient, String dbSnapshotIdentifier, String dbInstanceClusterIdentifier) { try { boolean snapshotReady = false; String snapshotReadyStr; System.out.println("Waiting for the snapshot to become available."); DescribeDbClusterSnapshotsRequest snapshotsRequest = DescribeDbClusterSnapshotsRequest.builder() .dbClusterSnapshotIdentifier(dbSnapshotIdentifier) .dbClusterIdentifier(dbInstanceClusterIdentifier) .build(); while (!snapshotReady) { DescribeDbClusterSnapshotsResponse response = rdsClient.describeDBClusterSnapshots(snapshotsRequest); List<DBClusterSnapshot> snapshotList = response.dbClusterSnapshots(); for (DBClusterSnapshot snapshot : snapshotList) { snapshotReadyStr = snapshot.status(); if (snapshotReadyStr.contains("available")) { snapshotReady = true; } else { System.out.println("."); Thread.sleep(sleepTime * 5000); } } } System.out.println("The Snapshot is available!"); } catch (RdsException | InterruptedException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 DescribeDBCluster 스냅샷을 참조하세요.
-
다음 코드 예시에서는 DescribeDBClusters
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void describeDbClusterParameters(RdsClient rdsClient, String dbCLusterGroupName, int flag) { try { DescribeDbClusterParametersRequest dbParameterGroupsRequest; if (flag == 0) { dbParameterGroupsRequest = DescribeDbClusterParametersRequest.builder() .dbClusterParameterGroupName(dbCLusterGroupName) .build(); } else { dbParameterGroupsRequest = DescribeDbClusterParametersRequest.builder() .dbClusterParameterGroupName(dbCLusterGroupName) .source("user") .build(); } DescribeDbClusterParametersResponse response = rdsClient .describeDBClusterParameters(dbParameterGroupsRequest); List<Parameter> dbParameters = response.parameters(); String paraName; for (Parameter para : dbParameters) { // Only print out information about either auto_increment_offset or // auto_increment_increment. paraName = para.parameterName(); if ((paraName.compareTo("auto_increment_offset") == 0) || (paraName.compareTo("auto_increment_increment ") == 0)) { System.out.println("*** The parameter name is " + paraName); System.out.println("*** The parameter value is " + para.parameterValue()); System.out.println("*** The parameter data type is " + para.dataType()); System.out.println("*** The parameter description is " + para.description()); System.out.println("*** The parameter allowed values is " + para.allowedValues()); } } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 DescribeDBClusters를 참조하세요.
-
다음 코드 예시에서는 DescribeDBEngineVersions
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void describeDBEngines(RdsClient rdsClient) { try { DescribeDbEngineVersionsRequest engineVersionsRequest = DescribeDbEngineVersionsRequest.builder() .engine("aurora-mysql") .defaultOnly(true) .maxRecords(20) .build(); DescribeDbEngineVersionsResponse response = rdsClient.describeDBEngineVersions(engineVersionsRequest); List<DBEngineVersion> engines = response.dbEngineVersions(); // Get all DBEngineVersion objects. for (DBEngineVersion engineOb : engines) { System.out.println("The name of the DB parameter group family for the database engine is " + engineOb.dbParameterGroupFamily()); System.out.println("The name of the database engine " + engineOb.engine()); System.out.println("The version number of the database engine " + engineOb.engineVersion()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 DescribeDBEngine 버전을 참조하세요.
-
다음 코드 예시에서는 DescribeDBInstances
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. // Waits until the database instance is available. public static void waitForInstanceReady(RdsClient rdsClient, String dbClusterIdentifier) { boolean instanceReady = false; String instanceReadyStr; System.out.println("Waiting for instance to become available."); try { DescribeDbClustersRequest instanceRequest = DescribeDbClustersRequest.builder() .dbClusterIdentifier(dbClusterIdentifier) .build(); while (!instanceReady) { DescribeDbClustersResponse response = rdsClient.describeDBClusters(instanceRequest); List<DBCluster> clusterList = response.dbClusters(); for (DBCluster cluster : clusterList) { instanceReadyStr = cluster.status(); if (instanceReadyStr.contains("available")) { instanceReady = true; } else { System.out.print("."); Thread.sleep(sleepTime * 1000); } } } System.out.println("Database cluster is available!"); } catch (RdsException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 DescribeDBInstances를 참조하세요.
-
다음 코드 예시에서는 DescribeOrderableDBInstanceOptions
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void describeDBEngines(RdsClient rdsClient) { try { DescribeDbEngineVersionsRequest engineVersionsRequest = DescribeDbEngineVersionsRequest.builder() .engine("aurora-mysql") .defaultOnly(true) .maxRecords(20) .build(); DescribeDbEngineVersionsResponse response = rdsClient.describeDBEngineVersions(engineVersionsRequest); List<DBEngineVersion> engines = response.dbEngineVersions(); // Get all DBEngineVersion objects. for (DBEngineVersion engineOb : engines) { System.out.println("The name of the DB parameter group family for the database engine is " + engineOb.dbParameterGroupFamily()); System.out.println("The name of the database engine " + engineOb.engine()); System.out.println("The version number of the database engine " + engineOb.engineVersion()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 DescribeOrderable 참조의 WordDBInstanceOptions를 참조하세요. AWS SDK for Java 2.x API
-
다음 코드 예시에서는 ModifyDBClusterParameterGroup
을 사용하는 방법을 보여 줍니다.
- Java 2.x용 SDK
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. public static void describeDbClusterParameterGroups(RdsClient rdsClient, String dbClusterGroupName) { try { DescribeDbClusterParameterGroupsRequest groupsRequest = DescribeDbClusterParameterGroupsRequest.builder() .dbClusterParameterGroupName(dbClusterGroupName) .maxRecords(20) .build(); List<DBClusterParameterGroup> groups = rdsClient.describeDBClusterParameterGroups(groupsRequest) .dbClusterParameterGroups(); for (DBClusterParameterGroup group : groups) { System.out.println("The group name is " + group.dbClusterParameterGroupName()); System.out.println("The group ARN is " + group.dbClusterParameterGroupArn()); } } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
API 세부 정보는 AWS SDK for Java 2.x API 참조의 ModifyDBClusterParameterGroupWord를 참조하세요.
-
시나리오
다음 코드 예제는 Amazon Aurora Serverless 데이터베이스의 작업 항목을 추적하고 Amazon Simple Email Service(Amazon SES)를 사용하여 보고서를 전송하는 웹 애플리케이션을 생성하는 방법을 보여줍니다.
- Java 2.x용 SDK
-
Amazon RDS 데이터베이스에 저장된 작업 항목을 추적하고 보고하는 웹 애플리케이션을 생성하는 방법을 보여줍니다.
Amazon Aurora Serverless 데이터를 쿼리하고 React 애플리케이션에서 사용할 Spring API REST를 설정하는 방법에 대한 전체 소스 코드 및 지침은 GitHub
의 전체 예제를 참조하세요. JDBCAPI를 사용하는 예제를 설정하고 실행하는 방법에 대한 전체 소스 코드 및 지침은 GitHub
의 전체 예제를 참조하세요. 이 예시에서 사용되는 서비스
Aurora
Amazon RDS
Amazon RDS 데이터 서비스
Amazon SES