There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.
Code examples for OpenSearch Service using AWS SDKs
The following code examples show you how to use Amazon OpenSearch Service with an AWS software development kit (SDK).
Basics are code examples that show you how to perform the essential operations within a service.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Get started
The following code example shows how to get started using OpenSearch Service.
- Java
-
- SDK for 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);
});
}
}