文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
与 AWS SDK CreateDBCluster
配合使用
以下代码示例演示如何使用 CreateDBCluster
。
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /** * Creates a new Amazon Neptune DB cluster asynchronously. * * @param dbName the name of the DB cluster to be created * @return a CompletableFuture that, when completed, provides the ID of the created DB cluster * @throws CompletionException if the operation fails for any reason, including if the request would exceed the maximum quota */ public CompletableFuture<String> createDBClusterAsync(String dbName) { CreateDbClusterRequest request = CreateDbClusterRequest.builder() .dbClusterIdentifier(dbName) .engine("neptune") .deletionProtection(false) .backupRetentionPeriod(1) .build(); return getAsyncClient().createDBCluster(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 cluster: " + exception.getMessage(), exception); } }) .thenApply(response -> { String clusterId = response.dbCluster().dbClusterIdentifier(); logger.info("DB Cluster created: " + clusterId); return clusterId; }); }
-
有关 API 的详细信息,请参阅DBCluster在 AWS SDK for Java 2.x API 参考中创建。
-
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 def create_db_cluster(neptune_client, db_name: str) -> str: """ Creates a Neptune DB cluster and returns its identifier. Args: neptune_client (boto3.client): The Neptune client object. db_name (str): The desired cluster identifier. Returns: str: The DB cluster identifier. Raises: RuntimeError: For any failure or AWS error, with a user-friendly message. """ request = { 'DBClusterIdentifier': db_name, 'Engine': 'neptune', 'DeletionProtection': False, 'BackupRetentionPeriod': 1 } try: response = neptune_client.create_db_cluster(**request) cluster = response.get('DBCluster') or {} cluster_id = cluster.get('DBClusterIdentifier') if not cluster_id: raise RuntimeError("Cluster created but no ID returned.") print(f"DB Cluster created: {cluster_id}") return cluster_id except ClientError as e: code = e.response["Error"]["Code"] message = e.response["Error"]["Message"] if code in ("ServiceQuotaExceededException", "DBClusterQuotaExceededFault"): raise RuntimeError("You have exceeded the quota for Neptune DB clusters.") from e else: raise RuntimeError(f"AWS error [{code}]: {message}") from e except Exception as e: raise RuntimeError(f"Unexpected error creating DB cluster '{db_name}': {e}") from e
-
有关 API 的详细信息,请参阅在 Python AWS 开发工具包DBCluster中创建 (Boto3) API 参考。
-
操作
CreateDBInstance