Doc AWS SDK ExamplesWord リポジトリには、さらに多くの GitHub の例があります。 AWS SDK
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
OpenSearch for Java 2.x を使用した SDK Service の例
次のコード例は、 OpenSearch Service AWS SDK for Java 2.x で を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。
「基本」は、重要なオペレーションをサービス内で実行する方法を示すコード例です。
アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。
各例には、完全なソースコードへのリンクが含まれています。ここでは、コンテキストでコードを設定および実行する方法の手順を確認できます。
開始方法
次のコード例は、 OpenSearch Service の使用を開始する方法を示しています。
- Java 2.x のSDK
-
注記
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 ドメインにタグを追加します。
OpenSearch Service ドメインに関連付けられたタグを一覧表示します。
OpenSearch Service ドメインからタグを削除します。
OpenSearch Service ドメインを削除します。
- Java 2.x のSDK
-
注記
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()); } }); } }
-
API の詳細については、AWS SDK for Java 2.x API リファレンスの以下のトピックを参照してください。
-
アクション
次のコード例は、AddTags
を使用する方法を示しています。
- Java 2.x のSDK
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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
-
注記
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 を参照してください。
-