文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Neptune 的代码示例 AWS SDKs
以下代码示例向您展示了如何使用带有 AWS 软件开发套件 (SDK) 的 Amazon Neptune。
基础知识是向您展示如何在服务中执行基本操作的代码示例。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
场景是向您展示如何通过在一个服务中调用多个函数或与其他 AWS 服务服务结合来完成特定任务的代码示例。
开始使用
以下代码示例显示了如何开始使用 Neptune。
- Java
-
- 适用于 Java 的 SDK 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());
}
});
}