OpenSearch Exemplos de serviços usando SDK para Java 2.x - AWS SDK for Java 2.x

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

OpenSearch Exemplos de serviços usando SDK para Java 2.x

Os exemplos de código a seguir mostram como realizar ações e implementar cenários comuns usando o AWS SDK for Java 2.x with OpenSearch Service.

As noções básicas são exemplos de código que mostram como realizar as operações essenciais em um serviço.

Ações são trechos de código de programas maiores e devem ser executadas em contexto. Embora as ações mostrem como chamar funções de serviço individuais, você pode ver as ações no contexto em seus cenários relacionados.

Cada exemplo inclui um link para o código-fonte completo, onde você pode encontrar instruções sobre como configurar e executar o código no contexto.

Conceitos básicos

O exemplo de código a seguir mostra como começar a usar o OpenSearch Service.

SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

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); }); } }
  • Para API obter detalhes, consulte ListVersionsem AWS SDK for Java 2.x APIReferência.

Conceitos básicos

O exemplo de código a seguir mostra como:

  • Crie um domínio OpenSearch de serviço.

  • Fornece informações detalhadas sobre um domínio OpenSearch de serviço específico.

  • Lista todos os domínios OpenSearch de serviço pertencentes à conta.

  • Espera até que o status de alteração do domínio do OpenSearch Serviço atinja um estado concluído.

  • Modifica a configuração de um domínio de OpenSearch serviço existente.

  • Adicione uma tag ao domínio do OpenSearch serviço.

  • Lista as tags associadas a um domínio OpenSearch de serviço.

  • Remove as tags de um domínio OpenSearch de serviço.

  • Exclui o domínio do OpenSearch serviço.

SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

Execute um cenário interativo demonstrando os recursos OpenSearch do Serviço.

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.services.opensearch.model.*; import java.util.List; import java.util.Scanner; import java.util.concurrent.CompletableFuture; public class OpenSearchScenario { public static final String DASHES = new String(new char[80]).replace("\0", "-"); private static final Logger logger = LoggerFactory.getLogger(OpenSearchScenario.class); static Scanner scanner = new Scanner(System.in); static OpenSearchActions openSearchActions = new OpenSearchActions(); public static void main(String[] args) throws Throwable { logger.info(""" Welcome to the Amazon OpenSearch Service Basics Scenario. Use the Amazon OpenSearch Service API to create, configure, and manage OpenSearch Service domains. The operations exposed by the AWS OpenSearch Service client are focused on managing the OpenSearch Service domains and their configurations, not the data within the domains (such as indexing or querying documents). For document management, you typically interact directly with the OpenSearch REST API or use other libraries, such as the OpenSearch Java client (https://opensearch.org/docs/latest/clients/java/). Let's get started... """); waitForInputToContinue(scanner); try { runScenario(); } catch (RuntimeException e) { e.printStackTrace(); } } private static void waitForInputToContinue(Scanner scanner) { while (true) { logger.info(""); logger.info("Enter 'c' followed by <ENTER> to continue:"); String input = scanner.nextLine(); if (input.trim().equalsIgnoreCase("c")) { logger.info("Continuing with the program..."); logger.info(""); break; } else { logger.info("Invalid input. Please try again."); } } } private static void runScenario() throws Throwable { String currentTimestamp = String.valueOf(System.currentTimeMillis()); String domainName = "test-domain-" + currentTimestamp; logger.info(DASHES); logger.info("1. Create an Amazon OpenSearch domain"); logger.info(""" An Amazon OpenSearch domain is a managed instance of the OpenSearch engine, which is an open-source search and analytics engine derived from Elasticsearch. An OpenSearch domain is essentially a cluster of compute resources and storage that hosts one or more OpenSearch indexes, enabling you to perform full-text searches, data analysis, and visualizations. In this step, we'll initiate the creation of the domain. We'll check on the progress in a later step. """); waitForInputToContinue(scanner); try { CompletableFuture<String> future = openSearchActions.createNewDomainAsync(domainName); String domainId = future.join(); logger.info("Domain successfully created with ID: {}", domainId); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause != null) { if (cause instanceof OpenSearchException openSearchEx) { logger.error("OpenSearch error occurred: Error message: {}, Error code {}", openSearchEx.awsErrorDetails().errorMessage(), openSearchEx.awsErrorDetails().errorCode()); } else { logger.error("An unexpected error occurred: " + cause.getMessage(), cause); } } else { logger.error("An unexpected error occurred: " + rt.getMessage()); } throw cause; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info("2. Describe the Amazon OpenSearch domain"); logger.info("In this step, we get back the Domain ARN which is used in an upcoming step."); waitForInputToContinue(scanner); String arn = ""; try { CompletableFuture<String> future = openSearchActions.describeDomainAsync(domainName); arn = future.join(); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof OpenSearchException openSearchEx) { logger.info("OpenSearch error occurred: Error message: {}, Error code {}", openSearchEx.awsErrorDetails().errorMessage(), openSearchEx.awsErrorDetails().errorCode()); } else { logger.info("An unexpected error occurred: " + rt.getMessage()); } throw cause; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info("3. List the domains in your account"); waitForInputToContinue(scanner); try { CompletableFuture<List<DomainInfo>> future = openSearchActions.listAllDomainsAsync(); List<DomainInfo> domainInfoList = future.join(); for (DomainInfo domain : domainInfoList) { logger.info("Domain name is: " + domain.domainName()); } } catch (RuntimeException rt) { Throwable cause = rt.getCause(); while (cause.getCause() != null && !(cause instanceof OpenSearchException)) { cause = cause.getCause(); } if (cause instanceof OpenSearchException openSearchEx) { logger.info("OpenSearch error occurred: Error message: {}, Error code {}", openSearchEx.awsErrorDetails().errorMessage(), openSearchEx.awsErrorDetails().errorCode()); } else { logger.info("An unexpected error occurred: " + rt.getMessage()); } throw cause; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info("4. Wait until the domain's change status reaches a completed state"); logger.info(""" In this step, we check on the change status of the domain that we initiated in Step 1. Until we reach a COMPLETED state, we stay in a loop by sending a DescribeDomainChangeProgressRequest. The time it takes for a change to an OpenSearch domain to reach a completed state can range from a few minutes to several hours. In this case the change is creating a new domain that we initiated in Step 1. The time varies depending on the complexity of the change and the current load on the OpenSearch service. In general, simple changes, such as scaling the number of data nodes or updating the OpenSearch version, may take 10-30 minutes. """); waitForInputToContinue(scanner); try { CompletableFuture<Void> future = openSearchActions.domainChangeProgressAsync(domainName); future.join(); logger.info("Domain change progress completed successfully."); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); while (cause.getCause() != null && !(cause instanceof ResourceNotFoundException)) { cause = cause.getCause(); } if (cause instanceof ResourceNotFoundException resourceNotFoundException) { logger.info("The specific AWS resource was not found: Error message: {}, Error code {}", resourceNotFoundException.awsErrorDetails().errorMessage(), resourceNotFoundException.awsErrorDetails().errorCode()); if (cause instanceof OpenSearchException ex) { logger.info("An OpenSearch error occurred: Error message: " + ex.getMessage()); } else { logger.info("An unexpected error occurred: " + rt.getMessage()); } throw cause; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info("5. Modify the domain"); logger.info(""" You can change your OpenSearch domain's settings, like the number of instances, without starting over from scratch. This makes it easy to adjust your domain as your needs change, allowing you to scale up or down quickly without recreating everything. We modify the domain in this step by changing the number of instances. """); waitForInputToContinue(scanner); try { CompletableFuture<UpdateDomainConfigResponse> future = openSearchActions.updateSpecificDomainAsync(domainName); UpdateDomainConfigResponse updateResponse = future.join(); logger.info("Domain update status: " + updateResponse.domainConfig().changeProgressDetails().configChangeStatusAsString()); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof OpenSearchException openSearchEx) { logger.info("OpenSearch error occurred: Error message: {}, Error code {}", openSearchEx.awsErrorDetails().errorMessage(), openSearchEx.awsErrorDetails().errorCode()); } else { logger.info("An unexpected error occurred: " + rt.getMessage()); } throw cause; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info("6. Wait until the domain's change status reaches a completed state"); logger.info(""" In this step, we poll the status until the domain's change status reaches a completed state. """); waitForInputToContinue(scanner); try { CompletableFuture<Void> future = openSearchActions.domainChangeProgressAsync(domainName); future.join(); logger.info("Domain change progress completed successfully."); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof OpenSearchException ex) { logger.info("EC2 error occurred: Error message: " +ex.getMessage()); } else { logger.info("An unexpected error occurred: " + rt.getMessage()); } throw cause; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info("7. Tag the Domain"); logger.info(""" Tags let you assign arbitrary information to an Amazon OpenSearch Service domain so you can categorize and filter on that information. A tag is a key-value pair that you define and associate with an OpenSearch Service domain. You can use these tags to track costs by grouping expenses for similarly tagged resources. In this scenario, we create tags with keys "service" and "instances". """); waitForInputToContinue(scanner); try { CompletableFuture<AddTagsResponse> future = openSearchActions.addDomainTagsAsync(arn); future.join(); logger.info("Domain tags added successfully."); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); while (cause.getCause() != null && !(cause instanceof OpenSearchException)) { cause = cause.getCause(); } if (cause instanceof OpenSearchException openSearchEx) { logger.info("OpenSearch error occurred: Error message: {}, Error code {}", openSearchEx.awsErrorDetails().errorMessage(), openSearchEx.awsErrorDetails().errorCode()); } else { logger.info("An unexpected error occurred: " + rt.getMessage()); if (cause != null) { if (cause instanceof OpenSearchException) { logger.error("OpenSearch error occurred: Error message: " + cause.getMessage(), cause); } else { logger.error("An unexpected error occurred: " + cause.getMessage(), cause); } } else { logger.error("An unexpected error occurred: " + rt.getMessage(), rt); } throw cause; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info("8. List Domain tags"); waitForInputToContinue(scanner); try { CompletableFuture<ListTagsResponse> future = openSearchActions.listDomainTagsAsync(arn); ListTagsResponse listTagsResponse = future.join(); listTagsResponse.tagList().forEach(tag -> logger.info("Tag Key: " + tag.key() + ", Tag Value: " + tag.value())); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); while (cause.getCause() != null && !(cause instanceof OpenSearchException)) { cause = cause.getCause(); } if (cause instanceof OpenSearchException openSearchEx) { logger.info("OpenSearch error occurred: Error message: {}, Error code {}", openSearchEx.awsErrorDetails().errorMessage(), openSearchEx.awsErrorDetails().errorCode()); } else { logger.info("An unexpected error occurred: " + rt.getMessage()); } throw cause; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info("9. Delete the domain"); logger.info(""" In this step, we'll delete the Amazon OpenSearch domain that we created in Step 1. Deleting a domain will remove all data and configuration for that domain. """); waitForInputToContinue(scanner); try { CompletableFuture<DeleteDomainResponse> future = openSearchActions.deleteSpecificDomainAsync(domainName); future.join(); logger.info("Domain successfully deleted."); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); while (cause.getCause() != null && !(cause instanceof OpenSearchException)) { cause = cause.getCause(); } if (cause instanceof OpenSearchException openSearchEx) { logger.info("OpenSearch error occurred: Error message: {}, Error code {}", openSearchEx.awsErrorDetails().errorMessage(), openSearchEx.awsErrorDetails().errorCode()); } else { logger.info("An unexpected error occurred: " + rt.getMessage()); } throw cause; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info("Scenario complete!"); } }

Uma classe de invólucro para SDK métodos OpenSearch de serviço.

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; import software.amazon.awssdk.core.retry.RetryPolicy; import software.amazon.awssdk.http.async.SdkAsyncHttpClient; import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.opensearch.OpenSearchAsyncClient; import software.amazon.awssdk.services.opensearch.model.AddTagsRequest; import software.amazon.awssdk.services.opensearch.model.AddTagsResponse; import software.amazon.awssdk.services.opensearch.model.ClusterConfig; import software.amazon.awssdk.services.opensearch.model.CreateDomainRequest; import software.amazon.awssdk.services.opensearch.model.DeleteDomainRequest; import software.amazon.awssdk.services.opensearch.model.DeleteDomainResponse; import software.amazon.awssdk.services.opensearch.model.DescribeDomainChangeProgressRequest; import software.amazon.awssdk.services.opensearch.model.DescribeDomainChangeProgressResponse; import software.amazon.awssdk.services.opensearch.model.DescribeDomainRequest; import software.amazon.awssdk.services.opensearch.model.DomainInfo; import software.amazon.awssdk.services.opensearch.model.DomainStatus; import software.amazon.awssdk.services.opensearch.model.EBSOptions; import software.amazon.awssdk.services.opensearch.model.ListDomainNamesRequest; import software.amazon.awssdk.services.opensearch.model.ListTagsRequest; import software.amazon.awssdk.services.opensearch.model.ListTagsResponse; import software.amazon.awssdk.services.opensearch.model.NodeToNodeEncryptionOptions; import software.amazon.awssdk.services.opensearch.model.Tag; import software.amazon.awssdk.services.opensearch.model.UpdateDomainConfigRequest; import software.amazon.awssdk.services.opensearch.model.UpdateDomainConfigResponse; import software.amazon.awssdk.services.opensearch.model.VolumeType; import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; public class OpenSearchActions { private static final Logger logger = LoggerFactory.getLogger(OpenSearchActions.class); private static OpenSearchAsyncClient openSearchClientAsyncClient; private static OpenSearchAsyncClient getAsyncClient() { if (openSearchClientAsyncClient == null) { SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(100) .connectionTimeout(Duration.ofSeconds(60)) .readTimeout(Duration.ofSeconds(60)) .writeTimeout(Duration.ofSeconds(60)) .build(); ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) .apiCallAttemptTimeout(Duration.ofSeconds(90)) .retryPolicy(RetryPolicy.builder() .numRetries(3) .build()) .build(); openSearchClientAsyncClient = OpenSearchAsyncClient.builder() .region(Region.US_EAST_1) .httpClient(httpClient) .overrideConfiguration(overrideConfig) .build(); } return openSearchClientAsyncClient; } /** * Creates a new OpenSearch domain asynchronously. * @param domainName the name of the new OpenSearch domain to create * @return a {@link CompletableFuture} containing the domain ID of the newly created domain */ public CompletableFuture<String> createNewDomainAsync(String domainName) { ClusterConfig clusterConfig = ClusterConfig.builder() .dedicatedMasterEnabled(true) .dedicatedMasterCount(3) .dedicatedMasterType("t2.small.search") .instanceType("t2.small.search") .instanceCount(5) .build(); EBSOptions ebsOptions = EBSOptions.builder() .ebsEnabled(true) .volumeSize(10) .volumeType(VolumeType.GP2) .build(); NodeToNodeEncryptionOptions encryptionOptions = NodeToNodeEncryptionOptions.builder() .enabled(true) .build(); CreateDomainRequest domainRequest = CreateDomainRequest.builder() .domainName(domainName) .engineVersion("OpenSearch_1.0") .clusterConfig(clusterConfig) .ebsOptions(ebsOptions) .nodeToNodeEncryptionOptions(encryptionOptions) .build(); logger.info("Sending domain creation request..."); return getAsyncClient().createDomain(domainRequest) .handle( (createResponse, throwable) -> { if (createResponse != null) { logger.info("Domain status is {}", createResponse.domainStatus().changeProgressDetails().configChangeStatusAsString()); logger.info("Domain Id is {}", createResponse.domainStatus().domainId()); return createResponse.domainStatus().domainId(); } throw new RuntimeException("Failed to create domain", throwable); }); } /** * Deletes a specific domain asynchronously. * @param domainName the name of the domain to be deleted * @return a {@link CompletableFuture} that completes when the domain has been deleted * or throws a {@link RuntimeException} if the deletion fails */ public CompletableFuture<DeleteDomainResponse> deleteSpecificDomainAsync(String domainName) { DeleteDomainRequest domainRequest = DeleteDomainRequest.builder() .domainName(domainName) .build(); // Delete domain asynchronously return getAsyncClient().deleteDomain(domainRequest) .whenComplete((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to delete the domain: " + domainName, exception); } }); } /** * Describes the specified domain asynchronously. * * @param domainName the name of the domain to describe * @return a {@link CompletableFuture} that completes with the ARN of the domain * @throws RuntimeException if the domain description fails */ public CompletableFuture<String> describeDomainAsync(String domainName) { DescribeDomainRequest request = DescribeDomainRequest.builder() .domainName(domainName) .build(); return getAsyncClient().describeDomain(request) .handle((response, exception) -> { // Handle both response and exception if (exception != null) { throw new RuntimeException("Failed to describe domain", exception); } DomainStatus domainStatus = response.domainStatus(); String endpoint = domainStatus.endpoint(); String arn = domainStatus.arn(); String engineVersion = domainStatus.engineVersion(); logger.info("Domain endpoint is: " + endpoint); logger.info("ARN: " + arn); System.out.println("Engine version: " + engineVersion); return arn; // Return ARN when successful }); } /** * Asynchronously lists all the domains in the current AWS account. * @return a {@link CompletableFuture} that, when completed, contains a list of {@link DomainInfo} objects representing * the domains in the account. * @throws RuntimeException if there was a failure while listing the domains. */ public CompletableFuture<List<DomainInfo>> listAllDomainsAsync() { ListDomainNamesRequest namesRequest = ListDomainNamesRequest.builder() .engineType("OpenSearch") .build(); return getAsyncClient().listDomainNames(namesRequest) .handle((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to list all domains", exception); } return response.domainNames(); // Return the list of domain names on success }); } /** * Updates the configuration of a specific domain asynchronously. * @param domainName the name of the domain to update * @return a {@link CompletableFuture} that represents the asynchronous operation of updating the domain configuration */ public CompletableFuture<UpdateDomainConfigResponse> updateSpecificDomainAsync(String domainName) { ClusterConfig clusterConfig = ClusterConfig.builder() .instanceCount(3) .build(); UpdateDomainConfigRequest updateDomainConfigRequest = UpdateDomainConfigRequest.builder() .domainName(domainName) .clusterConfig(clusterConfig) .build(); return getAsyncClient().updateDomainConfig(updateDomainConfigRequest) .whenComplete((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to update the domain configuration", exception); } // Handle success if needed (e.g., logging or additional actions) }); } /** * Asynchronously checks the progress of a domain change operation in Amazon OpenSearch Service. * @param domainName the name of the OpenSearch domain to check the progress for * @return a {@link CompletableFuture} that completes when the domain change operation is completed */ public CompletableFuture<Void> domainChangeProgressAsync(String domainName) { DescribeDomainChangeProgressRequest request = DescribeDomainChangeProgressRequest.builder() .domainName(domainName) .build(); return CompletableFuture.runAsync(() -> { boolean isCompleted = false; long startTime = System.currentTimeMillis(); while (!isCompleted) { try { // Handle the async client call using `join` to block synchronously for the result DescribeDomainChangeProgressResponse response = getAsyncClient() .describeDomainChangeProgress(request) .handle((resp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to check domain progress", ex); } return resp; }).join(); String state = response.changeProgressStatus().statusAsString(); // Get the status as string if ("COMPLETED".equals(state)) { logger.info("\nOpenSearch domain status: Completed"); isCompleted = true; } else { for (int i = 0; i < 5; i++) { long elapsedTimeInSeconds = (System.currentTimeMillis() - startTime) / 1000; String formattedTime = String.format("%02d:%02d", elapsedTimeInSeconds / 60, elapsedTimeInSeconds % 60); System.out.print("\rOpenSearch domain state: " + state + " | Time Elapsed: " + formattedTime + " "); System.out.flush(); Thread.sleep(1_000); } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException("Thread was interrupted", e); } } }); } /** * Asynchronously adds tags to an Amazon OpenSearch Service domain. * @param domainARN the Amazon Resource Name (ARN) of the Amazon OpenSearch Service domain to add tags to * @return a {@link CompletableFuture} that completes when the tags have been successfully added to the domain, * or throws a {@link RuntimeException} if the operation fails */ public CompletableFuture<AddTagsResponse> addDomainTagsAsync(String domainARN) { Tag tag1 = Tag.builder() .key("service") .value("OpenSearch") .build(); Tag tag2 = Tag.builder() .key("instances") .value("m3.2xlarge") .build(); List<Tag> tagList = new ArrayList<>(); tagList.add(tag1); tagList.add(tag2); AddTagsRequest addTagsRequest = AddTagsRequest.builder() .arn(domainARN) .tagList(tagList) .build(); return getAsyncClient().addTags(addTagsRequest) .whenComplete((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to add tags to the domain: " + domainARN, exception); } else { logger.info("Added Tags"); } }); } /** * Asynchronously lists the tags associated with the specified Amazon Resource Name (ARN). * @param arn the Amazon Resource Name (ARN) of the resource for which to list the tags * @return a {@link CompletableFuture} that, when completed, will contain a list of the tags associated with the * specified ARN * @throws RuntimeException if there is an error listing the tags */ public CompletableFuture<ListTagsResponse> listDomainTagsAsync(String arn) { ListTagsRequest tagsRequest = ListTagsRequest.builder() .arn(arn) .build(); return getAsyncClient().listTags(tagsRequest) .whenComplete((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to list domain tags", exception); } List<Tag> tagList = response.tagList(); for (Tag tag : tagList) { logger.info("Tag key is " + tag.key()); logger.info("Tag value is " + tag.value()); } }); } }

Ações

O código de exemplo a seguir mostra como usar AddTags.

SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/** * Asynchronously adds tags to an Amazon OpenSearch Service domain. * @param domainARN the Amazon Resource Name (ARN) of the Amazon OpenSearch Service domain to add tags to * @return a {@link CompletableFuture} that completes when the tags have been successfully added to the domain, * or throws a {@link RuntimeException} if the operation fails */ public CompletableFuture<AddTagsResponse> addDomainTagsAsync(String domainARN) { Tag tag1 = Tag.builder() .key("service") .value("OpenSearch") .build(); Tag tag2 = Tag.builder() .key("instances") .value("m3.2xlarge") .build(); List<Tag> tagList = new ArrayList<>(); tagList.add(tag1); tagList.add(tag2); AddTagsRequest addTagsRequest = AddTagsRequest.builder() .arn(domainARN) .tagList(tagList) .build(); return getAsyncClient().addTags(addTagsRequest) .whenComplete((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to add tags to the domain: " + domainARN, exception); } else { logger.info("Added Tags"); } }); }
  • Para API obter detalhes, consulte AddTagsem AWS SDK for Java 2.x APIReferência.

O código de exemplo a seguir mostra como usar ChangeProgress.

SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/** * Asynchronously checks the progress of a domain change operation in Amazon OpenSearch Service. * @param domainName the name of the OpenSearch domain to check the progress for * @return a {@link CompletableFuture} that completes when the domain change operation is completed */ public CompletableFuture<Void> domainChangeProgressAsync(String domainName) { DescribeDomainChangeProgressRequest request = DescribeDomainChangeProgressRequest.builder() .domainName(domainName) .build(); return CompletableFuture.runAsync(() -> { boolean isCompleted = false; long startTime = System.currentTimeMillis(); while (!isCompleted) { try { // Handle the async client call using `join` to block synchronously for the result DescribeDomainChangeProgressResponse response = getAsyncClient() .describeDomainChangeProgress(request) .handle((resp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to check domain progress", ex); } return resp; }).join(); String state = response.changeProgressStatus().statusAsString(); // Get the status as string if ("COMPLETED".equals(state)) { logger.info("\nOpenSearch domain status: Completed"); isCompleted = true; } else { for (int i = 0; i < 5; i++) { long elapsedTimeInSeconds = (System.currentTimeMillis() - startTime) / 1000; String formattedTime = String.format("%02d:%02d", elapsedTimeInSeconds / 60, elapsedTimeInSeconds % 60); System.out.print("\rOpenSearch domain state: " + state + " | Time Elapsed: " + formattedTime + " "); System.out.flush(); Thread.sleep(1_000); } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException("Thread was interrupted", e); } } }); }
  • Para API obter detalhes, consulte ChangeProgressem AWS SDK for Java 2.x APIReferência.

O código de exemplo a seguir mostra como usar CreateDomain.

SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/** * Creates a new OpenSearch domain asynchronously. * @param domainName the name of the new OpenSearch domain to create * @return a {@link CompletableFuture} containing the domain ID of the newly created domain */ public CompletableFuture<String> createNewDomainAsync(String domainName) { ClusterConfig clusterConfig = ClusterConfig.builder() .dedicatedMasterEnabled(true) .dedicatedMasterCount(3) .dedicatedMasterType("t2.small.search") .instanceType("t2.small.search") .instanceCount(5) .build(); EBSOptions ebsOptions = EBSOptions.builder() .ebsEnabled(true) .volumeSize(10) .volumeType(VolumeType.GP2) .build(); NodeToNodeEncryptionOptions encryptionOptions = NodeToNodeEncryptionOptions.builder() .enabled(true) .build(); CreateDomainRequest domainRequest = CreateDomainRequest.builder() .domainName(domainName) .engineVersion("OpenSearch_1.0") .clusterConfig(clusterConfig) .ebsOptions(ebsOptions) .nodeToNodeEncryptionOptions(encryptionOptions) .build(); logger.info("Sending domain creation request..."); return getAsyncClient().createDomain(domainRequest) .handle( (createResponse, throwable) -> { if (createResponse != null) { logger.info("Domain status is {}", createResponse.domainStatus().changeProgressDetails().configChangeStatusAsString()); logger.info("Domain Id is {}", createResponse.domainStatus().domainId()); return createResponse.domainStatus().domainId(); } throw new RuntimeException("Failed to create domain", throwable); }); }
  • Para API obter detalhes, consulte CreateDomainem AWS SDK for Java 2.x APIReferência.

O código de exemplo a seguir mostra como usar DeleteDomain.

SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/** * Deletes a specific domain asynchronously. * @param domainName the name of the domain to be deleted * @return a {@link CompletableFuture} that completes when the domain has been deleted * or throws a {@link RuntimeException} if the deletion fails */ public CompletableFuture<DeleteDomainResponse> deleteSpecificDomainAsync(String domainName) { DeleteDomainRequest domainRequest = DeleteDomainRequest.builder() .domainName(domainName) .build(); // Delete domain asynchronously return getAsyncClient().deleteDomain(domainRequest) .whenComplete((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to delete the domain: " + domainName, exception); } }); }
  • Para API obter detalhes, consulte DeleteDomainem AWS SDK for Java 2.x APIReferência.

O código de exemplo a seguir mostra como usar DescribeDomain.

SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/** * Updates the configuration of a specific domain asynchronously. * @param domainName the name of the domain to update * @return a {@link CompletableFuture} that represents the asynchronous operation of updating the domain configuration */ public CompletableFuture<UpdateDomainConfigResponse> updateSpecificDomainAsync(String domainName) { ClusterConfig clusterConfig = ClusterConfig.builder() .instanceCount(3) .build(); UpdateDomainConfigRequest updateDomainConfigRequest = UpdateDomainConfigRequest.builder() .domainName(domainName) .clusterConfig(clusterConfig) .build(); return getAsyncClient().updateDomainConfig(updateDomainConfigRequest) .whenComplete((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to update the domain configuration", exception); } // Handle success if needed (e.g., logging or additional actions) }); }
  • Para API obter detalhes, consulte DescribeDomainem AWS SDK for Java 2.x APIReferência.

O código de exemplo a seguir mostra como usar ListDomainNames.

SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/** * Asynchronously lists all the domains in the current AWS account. * @return a {@link CompletableFuture} that, when completed, contains a list of {@link DomainInfo} objects representing * the domains in the account. * @throws RuntimeException if there was a failure while listing the domains. */ public CompletableFuture<List<DomainInfo>> listAllDomainsAsync() { ListDomainNamesRequest namesRequest = ListDomainNamesRequest.builder() .engineType("OpenSearch") .build(); return getAsyncClient().listDomainNames(namesRequest) .handle((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to list all domains", exception); } return response.domainNames(); // Return the list of domain names on success }); }
  • Para API obter detalhes, consulte ListDomainNamesem AWS SDK for Java 2.x APIReferência.

O código de exemplo a seguir mostra como usar ListTags.

SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/** * Asynchronously adds tags to an Amazon OpenSearch Service domain. * @param domainARN the Amazon Resource Name (ARN) of the Amazon OpenSearch Service domain to add tags to * @return a {@link CompletableFuture} that completes when the tags have been successfully added to the domain, * or throws a {@link RuntimeException} if the operation fails */ public CompletableFuture<AddTagsResponse> addDomainTagsAsync(String domainARN) { Tag tag1 = Tag.builder() .key("service") .value("OpenSearch") .build(); Tag tag2 = Tag.builder() .key("instances") .value("m3.2xlarge") .build(); List<Tag> tagList = new ArrayList<>(); tagList.add(tag1); tagList.add(tag2); AddTagsRequest addTagsRequest = AddTagsRequest.builder() .arn(domainARN) .tagList(tagList) .build(); return getAsyncClient().addTags(addTagsRequest) .whenComplete((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to add tags to the domain: " + domainARN, exception); } else { logger.info("Added Tags"); } }); }
  • Para API obter detalhes, consulte ListTagsem AWS SDK for Java 2.x APIReferência.

O código de exemplo a seguir mostra como usar UpdateDomainConfig.

SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/** * Updates the configuration of a specific domain asynchronously. * @param domainName the name of the domain to update * @return a {@link CompletableFuture} that represents the asynchronous operation of updating the domain configuration */ public CompletableFuture<UpdateDomainConfigResponse> updateSpecificDomainAsync(String domainName) { ClusterConfig clusterConfig = ClusterConfig.builder() .instanceCount(3) .build(); UpdateDomainConfigRequest updateDomainConfigRequest = UpdateDomainConfigRequest.builder() .domainName(domainName) .clusterConfig(clusterConfig) .build(); return getAsyncClient().updateDomainConfig(updateDomainConfigRequest) .whenComplete((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to update the domain configuration", exception); } // Handle success if needed (e.g., logging or additional actions) }); }
  • Para API obter detalhes, consulte UpdateDomainConfigem AWS SDK for Java 2.x APIReferência.