Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 AWS
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
AWS SDKsコード例
次のコード例は、 AWS Software Development Kit (SDK) で Amazon Neptune を使用する方法を示しています。
基本は、重要なオペレーションをサービス内で実行する方法を示すコード例です。
アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。
「シナリオ」は、1 つのサービス内から、または他の AWS のサービスと組み合わせて複数の関数を呼び出し、特定のタスクを実行する方法を示すコード例です。
開始方法
次のコード例は、Neptune の使用を開始する方法を示しています。
- Java
-
- SDK for Java 2.x
-
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
*/
public class HelloNeptune {
public static void main(String[] args) {
NeptuneAsyncClient neptuneClient = NeptuneAsyncClient.create();
describeDbCluster(neptuneClient).join(); // This ensures the async code runs to completion
}
/**
* Describes the Amazon Neptune DB clusters.
*
* @param neptuneClient the Neptune asynchronous client used to make the request
* @return a {@link CompletableFuture} that completes when the operation is finished
*/
public static CompletableFuture<Void> describeDbCluster(NeptuneAsyncClient neptuneClient) {
DescribeDbClustersRequest request = DescribeDbClustersRequest.builder()
.maxRecords(20)
.build();
SdkPublisher<DescribeDbClustersResponse> paginator = neptuneClient.describeDBClustersPaginator(request);
CompletableFuture<Void> future = new CompletableFuture<>();
paginator.subscribe(new Subscriber<DescribeDbClustersResponse>() {
private Subscription subscription;
@Override
public void onSubscribe(Subscription s) {
this.subscription = s;
s.request(Long.MAX_VALUE); // request all items
}
@Override
public void onNext(DescribeDbClustersResponse response) {
response.dbClusters().forEach(cluster -> {
System.out.println("Cluster Identifier: " + cluster.dbClusterIdentifier());
System.out.println("Status: " + cluster.status());
});
}
@Override
public void onError(Throwable t) {
future.completeExceptionally(t);
}
@Override
public void onComplete() {
future.complete(null);
}
});
return future.whenComplete((result, throwable) -> {
neptuneClient.close();
if (throwable != null) {
System.err.println("Error describing DB clusters: " + throwable.getMessage());
}
});
}