使用 OpenSearch for Java 2.x 的 SDK Service 範例 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 OpenSearch for Java 2.x 的 SDK Service 範例

下列程式碼範例示範如何使用 AWS SDK for Java 2.x with OpenSearch Service 來執行動作和實作常見案例。

基本概念是程式碼範例,示範如何在服務內執行基本操作。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

開始使用

下列程式碼範例示範如何開始使用 OpenSearch Service。

Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 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); }); } }
  • 如需 API 詳細資訊,請參閱 ListVersions AWS SDK for Java 2.x 參考中的 API

基本概念

以下程式碼範例顯示做法:

  • 建立 OpenSearch Service 網域。

  • 提供有關特定 OpenSearch Service 網域的詳細資訊。

  • 列出 帳戶擁有的所有 OpenSearch Service 網域。

  • 等待 OpenSearch Service 網域的變更狀態達到已完成狀態。

  • 修改現有 OpenSearch Service 網域的組態。

  • 將標籤新增至 OpenSearch Service 網域。

  • 列出與 a OpenSearch Service 網域相關聯的標籤。

  • 將標籤從 a OpenSearch Service 網域中移除。

  • 刪除 OpenSearch Service 網域。

Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

執行顯示 OpenSearch Service 功能的互動式案例。

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!"); } }

OpenSearch Service SDK 方法的包裝程式類別。

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()); } }); } }

動作

下列程式碼範例示範如何使用 AddTags

Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 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"); } }); }
  • 如需 API 詳細資訊,請參閱 AddTags AWS SDK for Java 2.x 參考中的 API

下列程式碼範例示範如何使用 ChangeProgress

Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 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); } } }); }
  • 如需 API 詳細資訊,請參閱 ChangeProgress AWS SDK for Java 2.x 參考中的 API

下列程式碼範例示範如何使用 CreateDomain

Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 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); }); }
  • 如需 API 詳細資訊,請參閱 CreateDomain AWS SDK for Java 2.x 參考中的 API

下列程式碼範例示範如何使用 DeleteDomain

Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 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); } }); }
  • 如需 API 詳細資訊,請參閱 DeleteDomain AWS SDK for Java 2.x 參考中的 API

下列程式碼範例示範如何使用 DescribeDomain

Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 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) }); }
  • 如需 API 詳細資訊,請參閱 DescribeDomain AWS SDK for Java 2.x 參考中的 API

下列程式碼範例示範如何使用 ListDomainNames

Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 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 }); }
  • 如需 API 詳細資訊,請參閱 ListDomainNames AWS SDK for Java 2.x 參考中的 API

下列程式碼範例示範如何使用 ListTags

Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 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"); } }); }
  • 如需 API 詳細資訊,請參閱 ListTags AWS SDK for Java 2.x 參考中的 API

下列程式碼範例示範如何使用 UpdateDomainConfig

Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 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) }); }
  • 如需 API 詳細資訊,請參閱 UpdateDomainConfig AWS SDK for Java 2.x 參考中的 API