OpenSearch for Java 2.x를 사용한 SDK Service 예제 - AWS SDK 코드 예제

AWS Doc SDK ExamplesWord AWS SDK 리포지토리에는 더 많은 GitHub 예제가 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

OpenSearch for Java 2.x를 사용한 SDK Service 예제

다음 코드 예제에서는 AWS SDK for Java 2.x with OpenSearch Service를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

기본 사항은 서비스 내에서 필수 작업을 수행하는 방법을 보여주는 코드 예제입니다.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접적으로 호출하는 방법을 보여주며 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.

각 예제에는 컨텍스트에서 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있는 전체 소스 코드에 대한 링크가 포함되어 있습니다.

시작

다음 코드 예제에서는 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 서비스 도메인을 생성합니다.

  • 특정 OpenSearch 서비스 도메인에 대한 자세한 정보를 제공합니다.

  • 계정이 소유한 모든 OpenSearch Service 도메인을 나열합니다.

  • OpenSearch 서비스 도메인의 변경 상태가 완료 상태가 될 때까지 기다립니다.

  • 기존 OpenSearch Service 도메인의 구성을 수정합니다.

  • OpenSearch Service 도메인에 태그를 추가합니다.

  • OpenSearch 서비스 도메인과 연결된 태그를 나열합니다.

  • OpenSearch 서비스 도메인에서 태그를 제거합니다.

  • OpenSearch 서비스 도메인을 삭제합니다.

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를 참조하세요.