文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
与 AWS SDK CreateDBInstance
配合使用
以下代码示例演示如何使用 CreateDBInstance
。
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /** * Creates a new Amazon Neptune DB instance asynchronously. * * @param dbInstanceId the identifier for the new DB instance * @param dbClusterId the identifier for the DB cluster that the new instance will be a part of * @return a {@link CompletableFuture} that completes with the identifier of the newly created DB instance * @throws CompletionException if the operation fails, with a cause of either: * - {@link ServiceQuotaExceededException} if the request would exceed the maximum quota, or * - a general exception with the failure message */ public CompletableFuture<String> createDBInstanceAsync(String dbInstanceId, String dbClusterId) { CreateDbInstanceRequest request = CreateDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceId) .dbInstanceClass("db.r5.large") .engine("neptune") .dbClusterIdentifier(dbClusterId) .build(); return getAsyncClient().createDBInstance(request) .whenComplete((response, exception) -> { if (exception != null) { Throwable cause = exception.getCause(); if (cause instanceof ServiceQuotaExceededException) { throw new CompletionException("The operation was denied because the request would exceed the maximum quota.", cause); } throw new CompletionException("Failed to create Neptune DB instance: " + exception.getMessage(), exception); } }) .thenApply(response -> { String instanceId = response.dbInstance().dbInstanceIdentifier(); logger.info("Created Neptune DB Instance: " + instanceId); return instanceId; }); }
-
有关 API 的详细信息,请参阅DBInstance在 AWS SDK for Java 2.x API 参考中创建。
-
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 def create_db_instance(neptune_client, db_instance_id: str, db_cluster_id: str) -> str: try: request = { 'DBInstanceIdentifier': db_instance_id, 'DBInstanceClass': 'db.r5.large', 'Engine': 'neptune', 'DBClusterIdentifier': db_cluster_id } print(f"Creating Neptune DB Instance: {db_instance_id}") response = neptune_client.create_db_instance(**request) instance = response.get('DBInstance') if not instance or 'DBInstanceIdentifier' not in instance: raise RuntimeError("Instance creation succeeded but no ID returned.") print(f"Waiting for DB Instance '{db_instance_id}' to become available...") waiter = neptune_client.get_waiter('db_instance_available') waiter.wait( DBInstanceIdentifier=db_instance_id, WaiterConfig={'Delay': 30, 'MaxAttempts': 40} ) print(f"DB Instance '{db_instance_id}' is now available.") return instance['DBInstanceIdentifier'] except ClientError as err: code = err.response["Error"]["Code"] message = err.response["Error"]["Message"] if code == "AccessDeniedException": print("Access denied. Please ensure you have the necessary permissions.") else: print(f"Couldn't create DB instance. Here's why: {code}: {message}") raise except Exception as e: print(f"Unexpected error creating DB instance '{db_instance_id}': {e}") raise RuntimeError(f"Unexpected error creating DB instance '{db_instance_id}': {e}") from e
-
有关 API 的详细信息,请参阅在 Python AWS 开发工具包DBInstance中创建 (Boto3) API 参考。
-
CreateDBCluster
CreateDBSubnetGroup