Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc
Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Gunakan DescribeDBInstances
dengan AWS SDK
Contoh kode berikut menunjukkan cara menggunakanDescribeDBInstances
.
- Java
-
- SDK untuk Java 2.x
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS
. /** * Checks the status of a Neptune instance recursively until the desired status is reached or a timeout occurs. * * @param instanceId the ID of the Neptune instance to check * @param desiredStatus the desired status of the Neptune instance * @param startTime the start time of the operation, used to calculate the elapsed time * @param future a {@link CompletableFuture} that will be completed when the desired status is reached */ private void checkStatusRecursive(String instanceId, String desiredStatus, long startTime, CompletableFuture<Void> future) { DescribeDbInstancesRequest request = DescribeDbInstancesRequest.builder() .dbInstanceIdentifier(instanceId) .build(); getAsyncClient().describeDBInstances(request) .whenComplete((response, exception) -> { if (exception != null) { Throwable cause = exception.getCause(); future.completeExceptionally( new CompletionException("Error checking Neptune instance status", cause) ); return; } List<DBInstance> instances = response.dbInstances(); if (instances.isEmpty()) { future.completeExceptionally(new RuntimeException("Instance not found: " + instanceId)); return; } String currentStatus = instances.get(0).dbInstanceStatus(); long elapsedSeconds = (System.currentTimeMillis() - startTime) / 1000; System.out.printf("\r Elapsed: %-20s Status: %-20s", formatElapsedTime((int) elapsedSeconds), currentStatus); System.out.flush(); if (desiredStatus.equalsIgnoreCase(currentStatus)) { System.out.printf("\r Neptune instance reached desired status '%s' after %s.\n", desiredStatus, formatElapsedTime((int) elapsedSeconds)); future.complete(null); } else { CompletableFuture.delayedExecutor(20, TimeUnit.SECONDS) .execute(() -> checkStatusRecursive(instanceId, desiredStatus, startTime, future)); } }); }
-
Untuk detail API, lihat Menjelaskan DBInstances di Referensi AWS SDK for Java 2.x API.
-
- Python
-
- SDK untuk Python (Boto3)
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS
. def check_instance_status(neptune_client, instance_id: str, desired_status: str): """ Polls the status of a Neptune DB instance until it reaches desired_status. Uses pagination via describe_db_instances — even for a single instance. Raises: ClientError: If describe_db_instances fails (e.g., instance not found). RuntimeError: If timeout expires before reaching desired status. """ paginator = neptune_client.get_paginator('describe_db_instances') start_time = time.time() while True: try: pages = paginator.paginate(DBInstanceIdentifier=instance_id) instances = [] for page in pages: instances.extend(page.get('DBInstances', [])) except ClientError as err: code = err.response["Error"]["Code"] message = err.response["Error"]["Message"] if code == "DBInstanceNotFound": print(f"Instance '{instance_id}' not found. Please verify the instance ID.") else: print(f"Failed to describe DB instance. {code}: {message}") raise current_status = instances[0].get('DBInstanceStatus') if instances else None elapsed = int(time.time() - start_time) print(f"\rElapsed: {format_elapsed_time(elapsed)} Status: {current_status}", end="", flush=True) if current_status and current_status.lower() == desired_status.lower(): print(f"\nInstance '{instance_id}' reached '{desired_status}' in {format_elapsed_time(elapsed)}.") return if elapsed > TIMEOUT_SECONDS: raise RuntimeError(f"Timeout waiting for '{instance_id}' to reach '{desired_status}'") time.sleep(POLL_INTERVAL_SECONDS)
-
Untuk detail API, lihat Menjelaskan DBInstances dalam AWS SDK for Python (Boto3) Referensi API.
-