本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
OpenSearch SDK适用于 Java 2.x 的服务示例
以下代码示例向您展示了如何使用 with S OpenSearch ervice 来执行操作和实现常见场景。 Amazon SDK for Java 2.x
基础知识是向您展示如何在服务中执行基本操作的代码示例。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。
开始使用
以下代码示例显示了如何开始使用 S OpenSearch ervice。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 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详细信息,请参阅 “Amazon SDK for Java 2.x API参考 ListVersions” 中的。
-
基础知识
以下代码示例展示了如何:
创建 OpenSearch 服务域。
提供有关特定 OpenSearch 服务域的详细信息。
列出该账户拥有的所有 OpenSearch 服务域。
等待,直到 OpenSearch 服务域的更改状态达到已完成状态。
修改现有 OpenSearch 服务域的配置。
向 OpenSearch 服务域添加标签。
列出与 OpenSearch 服务域关联的标签。
从 OpenSearch 服务域中移除标签。
删除 OpenSearch 服务域。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 运行演示 OpenSearch 服务功能的交互式场景。
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 服务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()); } }); } }
-
有关API详细信息,请参阅 “参Amazon SDK for Java 2.x API考” 中的以下主题。
-
操作
以下代码示例演示如何使用 AddTags
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 /** * 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详细信息,请参阅 “Amazon SDK for Java 2.x API参考 AddTags” 中的。
-
以下代码示例演示如何使用 ChangeProgress
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 /** * 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详细信息,请参阅 “Amazon SDK for Java 2.x API参考 ChangeProgress” 中的。
-
以下代码示例演示如何使用 CreateDomain
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 /** * 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详细信息,请参阅 “Amazon SDK for Java 2.x API参考 CreateDomain” 中的。
-
以下代码示例演示如何使用 DeleteDomain
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 /** * 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详细信息,请参阅 “Amazon SDK for Java 2.x API参考 DeleteDomain” 中的。
-
以下代码示例演示如何使用 DescribeDomain
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 /** * 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详细信息,请参阅 “Amazon SDK for Java 2.x API参考 DescribeDomain” 中的。
-
以下代码示例演示如何使用 ListDomainNames
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 /** * 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详细信息,请参阅 “Amazon SDK for Java 2.x API参考 ListDomainNames” 中的。
-
以下代码示例演示如何使用 ListTags
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 /** * 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详细信息,请参阅 “Amazon SDK for Java 2.x API参考 ListTags” 中的。
-
以下代码示例演示如何使用 UpdateDomainConfig
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 /** * 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详细信息,请参阅 “Amazon SDK for Java 2.x API参考 UpdateDomainConfig” 中的。
-