Amazon Elasticsearch Service
开发人员指南 (API 版本 2015-01-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

在 Amazon Elasticsearch Service 中使用 AWS 开发工具包

本章包含如何使用 AWS 开发工具包与 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 take automated snapshots at noon and 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) .withSnapshotOptions(new SnapshotOptions() .withAutomatedSnapshotStartHour(12)) // .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()); } }

本页内容: