文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 AWS SDKs Neptune 程式碼範例
下列程式碼範例示範如何使用 Amazon Neptune 搭配 AWS 軟體開發套件 (SDK)。
基本概念是程式碼範例,這些範例說明如何在服務內執行基本操作。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。
案例是向您展示如何呼叫服務中的多個函數或與其他 AWS 服務組合來完成特定任務的程式碼範例。
開始使用
下列程式碼範例示範如何開始使用 Neptune。
- Java
-
- SDK for Java 2.x
-
/**
* 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());
}
});
}