Amazon Elasticsearch Service
開発者ガイド (API バージョン 2015-01-01)

Amazon Elasticsearch Service での AWS SDK の使用

この章には、AWS SDK を使用して Amazon Elasticsearch Service 設定 API を操作する方法の例が含まれています。これらのコードサンプルは、Amazon ES ドメインの作成、更新、削除方法を示しています。

重要

Elasticsearch API (_index_bulk_search_snapshot など) を操作する方法の例については、「Amazon Elasticsearch Service への HTTP リクエストの署名」を参照してください。

Java

この最初の例では、AWS SDK for Java を使用してドメインの作成、その設定の更新、および削除を行います。waitForDomainProcessing への呼び出しのコメント (および deleteDomain への呼び出しのコメント) を削除し、ドメインをオンラインにして使用できるようにします。

package com.amazonaws.samples; import java.util.concurrent.TimeUnit; import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; import com.amazonaws.regions.Regions; import com.amazonaws.services.elasticsearch.AWSElasticsearch; import com.amazonaws.services.elasticsearch.AWSElasticsearchClientBuilder; import com.amazonaws.services.elasticsearch.model.CreateElasticsearchDomainRequest; import com.amazonaws.services.elasticsearch.model.CreateElasticsearchDomainResult; import com.amazonaws.services.elasticsearch.model.DeleteElasticsearchDomainRequest; import com.amazonaws.services.elasticsearch.model.DeleteElasticsearchDomainResult; import com.amazonaws.services.elasticsearch.model.DescribeElasticsearchDomainRequest; import com.amazonaws.services.elasticsearch.model.DescribeElasticsearchDomainResult; import com.amazonaws.services.elasticsearch.model.EBSOptions; import com.amazonaws.services.elasticsearch.model.ElasticsearchClusterConfig; import com.amazonaws.services.elasticsearch.model.NodeToNodeEncryptionOptions; import com.amazonaws.services.elasticsearch.model.ResourceNotFoundException; import com.amazonaws.services.elasticsearch.model.SnapshotOptions; import com.amazonaws.services.elasticsearch.model.UpdateElasticsearchDomainConfigRequest; import com.amazonaws.services.elasticsearch.model.UpdateElasticsearchDomainConfigResult; import com.amazonaws.services.elasticsearch.model.VolumeType; /** * Sample class demonstrating how to use the AWS SDK for Java to create, update, * and delete Amazon Elasticsearch Service domains. */ public class AESSample { public static void main(String[] args) { final String domainName = "my-test-domain"; // Build the client using the default credentials chain. // You can use the AWS CLI and run `aws configure` to set access key, secret // key, and default region. final AWSElasticsearch client = AWSElasticsearchClientBuilder .standard() // Unnecessary, but lets you use a region different than your default. .withRegion(Regions.US_WEST_2) // Unnecessary, but if desired, you can use a different provider chain. .withCredentials(new DefaultAWSCredentialsProviderChain()) .build(); // Create a new domain, update its configuration, and delete it. createDomain(client, domainName); // waitForDomainProcessing(client, domainName); updateDomain(client, domainName); // waitForDomainProcessing(client, domainName); deleteDomain(client, domainName); } /** * Creates an Amazon Elasticsearch Service domain with the specified options. * Some options require other AWS resources, such as an Amazon Cognito user pool * and identity pool, whereas others require just an instance type or instance * count. * * @param client * The AWSElasticsearch client to use for the requests to Amazon * Elasticsearch Service * @param domainName * The name of the domain you want to create */ private static void createDomain(final AWSElasticsearch client, final String domainName) { // Create the request and set the desired configuration options CreateElasticsearchDomainRequest createRequest = new CreateElasticsearchDomainRequest() .withDomainName(domainName) .withElasticsearchVersion("6.3") .withElasticsearchClusterConfig(new ElasticsearchClusterConfig() .withDedicatedMasterEnabled(true) .withDedicatedMasterCount(3) // Small, inexpensive instance types for testing. Not recommended for production // domains. .withDedicatedMasterType("t2.small.elasticsearch") .withInstanceType("t2.small.elasticsearch") .withInstanceCount(5)) // Many instance types require EBS storage. .withEBSOptions(new EBSOptions() .withEBSEnabled(true) .withVolumeSize(10) .withVolumeType(VolumeType.Gp2)) // You can uncomment this line and add your account ID, a user name, and the // domain name to add an access policy. // .withAccessPolicies("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"arn:aws:iam::123456789012:user/user-name\"]},\"Action\":[\"es:*\"],\"Resource\":\"arn:aws:es:region:123456789012:domain/domain-name/*\"}]}") .withNodeToNodeEncryptionOptions(new NodeToNodeEncryptionOptions() .withEnabled(true)); // Make the request. System.out.println("Sending domain creation request..."); CreateElasticsearchDomainResult createResponse = client.createElasticsearchDomain(createRequest); System.out.println("Domain creation response from Amazon Elasticsearch Service:"); System.out.println(createResponse.getDomainStatus().toString()); } /** * Updates the configuration of an Amazon Elasticsearch Service domain with the * specified options. Some options require other AWS resources, such as an * Amazon Cognito user pool and identity pool, whereas others require just an * instance type or instance count. * * @param client * The AWSElasticsearch client to use for the requests to Amazon * Elasticsearch Service * @param domainName * The name of the domain to update */ private static void updateDomain(final AWSElasticsearch client, final String domainName) { try { // Updates the domain to use three data instances instead of five. // You can uncomment the Cognito lines and fill in the strings to enable Cognito // authentication for Kibana. final UpdateElasticsearchDomainConfigRequest updateRequest = new UpdateElasticsearchDomainConfigRequest() .withDomainName(domainName) // .withCognitoOptions(new CognitoOptions() // .withEnabled(true) // .withUserPoolId("user-pool-id") // .withIdentityPoolId("identity-pool-id") // .withRoleArn("role-arn")) .withElasticsearchClusterConfig(new ElasticsearchClusterConfig() .withInstanceCount(3)); System.out.println("Sending domain update request..."); final UpdateElasticsearchDomainConfigResult updateResponse = client .updateElasticsearchDomainConfig(updateRequest); System.out.println("Domain update response from Amazon Elasticsearch Service:"); System.out.println(updateResponse.toString()); } catch (ResourceNotFoundException e) { System.out.println("Domain not found. Please check the domain name."); } } /** * Deletes an Amazon Elasticsearch Service domain. Deleting a domain can take * several minutes. * * @param client * The AWSElasticsearch client to use for the requests to Amazon * Elasticsearch Service * @param domainName * The name of the domain that you want to delete */ private static void deleteDomain(final AWSElasticsearch client, final String domainName) { try { final DeleteElasticsearchDomainRequest deleteRequest = new DeleteElasticsearchDomainRequest() .withDomainName(domainName); System.out.println("Sending domain deletion request..."); final DeleteElasticsearchDomainResult deleteResponse = client.deleteElasticsearchDomain(deleteRequest); System.out.println("Domain deletion response from Amazon Elasticsearch Service:"); System.out.println(deleteResponse.toString()); } catch (ResourceNotFoundException e) { System.out.println("Domain not found. Please check the domain name."); } } /** * Waits for the domain to finish processing changes. New domains typically take * 10-15 minutes to initialize. Most updates to existing domains take a similar * amount of time. This method checks every 15 seconds and finishes only when * the domain's processing status changes to false. * * @param client * The AWSElasticsearch client to use for the requests to Amazon * Elasticsearch Service * @param domainName * The name of the domain that you want to check */ private static void waitForDomainProcessing(final AWSElasticsearch client, final String domainName) { // Create a new request to check the domain status. final DescribeElasticsearchDomainRequest describeRequest = new DescribeElasticsearchDomainRequest() .withDomainName(domainName); // Check whether the domain is processing, which usually takes 10-15 minutes // after creation or a configuration change. // This loop checks every 15 seconds. DescribeElasticsearchDomainResult describeResponse = client.describeElasticsearchDomain(describeRequest); while (describeResponse.getDomainStatus().isProcessing()) { try { System.out.println("Domain still processing..."); TimeUnit.SECONDS.sleep(15); describeResponse = client.describeElasticsearchDomain(describeRequest); } catch (InterruptedException e) { e.printStackTrace(); } } // Once we exit that loop, the domain is available System.out.println("Amazon Elasticsearch Service has finished processing changes for your domain."); System.out.println("Domain description response from Amazon Elasticsearch Service:"); System.out.println(describeResponse.toString()); } }

このページの内容: