Amazon Elasticsearch Service
개발자 가이드 (API 버전 2015-01-01)

Amazon Elasticsearch Service와 함께 AWS SDK 사용

이 장에는 AWS SDK를 사용하여 Amazon Elasticsearch Service 구성 API와 상호작용하는 방법의 예제가 나와 있습니다. 이러한 코드 샘플은 Amazon ES 도메인을 생성, 업데이트, 삭제하는 방법을 보여 줍니다.

중요

_index, _bulk, _search_snapshot 등 Elasticsearch API와 상호 작용하는 방법에 대한 예제는 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()); } }

이 페이지에서: