AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
OpenSearch 服务的代码示例 AWS SDKs
以下代码示例向您展示了如何使用带有 AWS 软件开发套件 (SDK) 的 Amazon OpenSearch 服务。
基础知识是向您展示如何在服务中执行基本操作的代码示例。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
开始使用
以下代码示例显示了如何开始使用 S OpenSearch ervice。
- Java
-
- SDK适用于 Java 2.x
-
import software.amazon.awssdk.services.opensearch.OpenSearchAsyncClient;
import software.amazon.awssdk.services.opensearch.model.ListVersionsRequest;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* 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 HelloOpenSearch {
public static void main(String[] args) {
try {
CompletableFuture<Void> future = listVersionsAsync();
future.join();
System.out.println("Versions listed successfully.");
} catch (RuntimeException e) {
System.err.println("Error occurred while listing versions: " + e.getMessage());
}
}
private static OpenSearchAsyncClient getAsyncClient() {
return OpenSearchAsyncClient.builder().build();
}
public static CompletableFuture<Void> listVersionsAsync() {
ListVersionsRequest request = ListVersionsRequest.builder()
.maxResults(10)
.build();
return getAsyncClient().listVersions(request).thenAccept(response -> {
List<String> versionList = response.versions();
for (String version : versionList) {
System.out.println("Version info: " + version);
}
}).exceptionally(ex -> {
// Handle the exception, or propagate it as a RuntimeException
throw new RuntimeException("Failed to list versions", ex);
});
}
}