翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Amazon OpenSearch Service を操作するための AWS SDKの使用
このセクションには、AWS SDK を使用して Amazon OpenSearch Service 設定 API を操作する方法の例が含まれています。これらのコードサンプルは、OpenSearch Service ドメインの作成、更新、削除方法を示しています。
Java
このセクションには、AWS SDK for Java のバージョン 1 および 2 の例が含まれています。
- Version 2
-
この例では、OpenSearchClientBuilder コンストラクタを使用して、AWS SDK for Java のバージョン 2 から OpenSearch ドメインの作成、その設定の更新、および削除を行います。waitForDomainProcessing
への呼び出しのコメント (および deleteDomain
への呼び出しのコメント) を削除し、ドメインをオンラインにして使用できるようにします。
package com.example.samples;
import java.util.concurrent.TimeUnit;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.opensearch.OpenSearchClient;
import software.amazon.awssdk.services.opensearch.model.ClusterConfig;
import software.amazon.awssdk.services.opensearch.model.EBSOptions;
import software.amazon.awssdk.services.opensearch.model.CognitoOptions;
import software.amazon.awssdk.services.opensearch.model.NodeToNodeEncryptionOptions;
import software.amazon.awssdk.services.opensearch.model.CreateDomainRequest;
import software.amazon.awssdk.services.opensearch.model.CreateDomainResponse;
import software.amazon.awssdk.services.opensearch.model.DescribeDomainRequest;
import software.amazon.awssdk.services.opensearch.model.UpdateDomainConfigRequest;
import software.amazon.awssdk.services.opensearch.model.UpdateDomainConfigResponse;
import software.amazon.awssdk.services.opensearch.model.DescribeDomainResponse;
import software.amazon.awssdk.services.opensearch.model.DeleteDomainRequest;
import software.amazon.awssdk.services.opensearch.model.DeleteDomainResponse;
import software.amazon.awssdk.services.opensearch.model.OpenSearchException;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
/**
* Sample class demonstrating how to use the Amazon Web Services SDK for Java to create, update,
* and delete Amazon OpenSearch Service domains.
*/
public class OpenSearchSample {
public static void main(String[] args) {
String domainName = "my-test-domain";
// Build the client using the default credentials chain.
// You can use the CLI and run `aws configure` to set access key, secret
// key, and default region.
OpenSearchClient client = OpenSearchClient.builder()
// Unnecessary, but lets you use a region different than your default.
.region(Region.US_EAST_1)
// Unnecessary, but if desired, you can use a different provider chain.
.credentialsProvider(DefaultCredentialsProvider.create())
.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 OpenSearch Service domain with the specified options.
* Some options require other Amazon Web Services resources, such as an Amazon Cognito user pool
* and identity pool, whereas others require just an instance type or instance
* count.
*
* @param client
* The client to use for the requests to Amazon OpenSearch Service
* @param domainName
* The name of the domain you want to create
*/
public static void createDomain(OpenSearchClient client, String domainName) {
// Create the request and set the desired configuration options
try {
ClusterConfig clusterConfig = ClusterConfig.builder()
.dedicatedMasterEnabled(true)
.dedicatedMasterCount(3)
// Small, inexpensive instance types for testing. Not recommended for production.
.dedicatedMasterType("t2.small.search")
.instanceType("t2.small.search")
.instanceCount(5)
.build();
// Many instance types require EBS storage.
EBSOptions ebsOptions = EBSOptions.builder()
.ebsEnabled(true)
.volumeSize(10)
.volumeType("gp2")
.build();
NodeToNodeEncryptionOptions encryptionOptions = NodeToNodeEncryptionOptions.builder()
.enabled(true)
.build();
CreateDomainRequest createRequest = CreateDomainRequest.builder()
.domainName(domainName)
.engineVersion("OpenSearch_1.0")
.clusterConfig(clusterConfig)
.ebsOptions(ebsOptions)
.nodeToNodeEncryptionOptions(encryptionOptions)
// You can uncomment this line and add your account ID, a username, and the
// domain name to add an access policy.
// .accessPolicies("{\"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/*\"}]}")
.build();
// Make the request.
System.out.println("Sending domain creation request...");
CreateDomainResponse createResponse = client.createDomain(createRequest);
System.out.println("Domain status: "+createResponse.domainStatus().toString());
System.out.println("Domain ID: "+createResponse.domainStatus().domainId());
} catch (OpenSearchException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
/**
* Updates the configuration of an Amazon OpenSearch Service domain with the
* specified options. Some options require other Amazon Web Services resources, such as an
* Amazon Cognito user pool and identity pool, whereas others require just an
* instance type or instance count.
*
* @param client
* The client to use for the requests to Amazon OpenSearch Service
* @param domainName
* The name of the domain to update
*/
public static void updateDomain(OpenSearchClient client, String domainName) {
// Updates the domain to use three data instances instead of five.
// You can uncomment the Cognito line and fill in the strings to enable Cognito
// authentication for OpenSearch Dashboards.
try {
ClusterConfig clusterConfig = ClusterConfig.builder()
.instanceCount(5)
.build();
CognitoOptions cognitoOptions = CognitoOptions.builder()
.enabled(true)
.userPoolId("user-pool-id")
.identityPoolId("identity-pool-id")
.roleArn("role-arn")
.build();
UpdateDomainConfigRequest updateRequest = UpdateDomainConfigRequest.builder()
.domainName(domainName)
.clusterConfig(clusterConfig)
//.cognitoOptions(cognitoOptions)
.build();
System.out.println("Sending domain update request...");
UpdateDomainConfigResponse updateResponse = client.updateDomainConfig(updateRequest);
System.out.println("Domain config: "+updateResponse.domainConfig().toString());
} catch (OpenSearchException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
/**
* Deletes an Amazon OpenSearch Service domain. Deleting a domain can take
* several minutes.
*
* @param client
* The client to use for the requests to Amazon OpenSearch Service
* @param domainName
* The name of the domain that you want to delete
*/
public static void deleteDomain(OpenSearchClient client, String domainName) {
try {
DeleteDomainRequest deleteRequest = DeleteDomainRequest.builder()
.domainName(domainName)
.build();
System.out.println("Sending domain deletion request...");
DeleteDomainResponse deleteResponse = client.deleteDomain(deleteRequest);
System.out.println("Domain status: "+deleteResponse.toString());
} catch (OpenSearchException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
/**
* Waits for the domain to finish processing changes. New domains typically take 15-30 minutes
* to initialize, but can take longer depending on the configuration. 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 client to use for the requests to Amazon OpenSearch Service
* @param domainName
* The name of the domain that you want to check
*/
public static void waitForDomainProcessing(OpenSearchClient client, String domainName) {
// Create a new request to check the domain status.
DescribeDomainRequest describeRequest = DescribeDomainRequest.builder()
.domainName(domainName)
.build();
// Every 15 seconds, check whether the domain is processing.
DescribeDomainResponse describeResponse = client.describeDomain(describeRequest);
while (describeResponse.domainStatus().processing()) {
try {
System.out.println("Domain still processing...");
TimeUnit.SECONDS.sleep(15);
describeResponse = client.describeDomain(describeRequest);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Once we exit that loop, the domain is available
System.out.println("Amazon OpenSearch Service has finished processing changes for your domain.");
System.out.println("Domain description: "+describeResponse.toString());
}
}
- Version 1
-
この例では AWSElasticsearchClientBuilder を使用して、AWS SDK for Java のバージョン 1 からレガシーmp Elasticsearch ドメインの作成、その設定の更新、および削除を行います。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.ResourceNotFoundException;
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 Amazon Web Services SDK for Java to create, update,
* and delete Amazon OpenSearch Service domains.
*/
public class OpenSearchSample {
public static void main(String[] args) {
final String domainName = "my-test-domain";
// Build the client using the default credentials chain.
// You can use the 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 OpenSearch Service domain with the specified options.
* Some options require other Amazon Web Services resources, such as an Amazon Cognito user pool
* and identity pool, whereas others require just an instance type or instance
* count.
*
* @param client
* The client to use for the requests to Amazon OpenSearch 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("7.10")
.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 username, 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/*\"}]}")
// Make the request.
System.out.println("Sending domain creation request...");
CreateElasticsearchDomainResult createResponse = client.createElasticsearchDomain(createRequest);
System.out.println("Domain creation response from Amazon OpenSearch Service:");
System.out.println(createResponse.getDomainStatus().toString());
}
/**
* Updates the configuration of an Amazon OpenSearch Service domain with the
* specified options. Some options require other Amazon Web Services resources, such as an
* Amazon Cognito user pool and identity pool, whereas others require just an
* instance type or instance count.
*
* @param client
* The client to use for the requests to Amazon OpenSearch 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 OpenSearch Dashboards.
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 OpenSearch Service:");
System.out.println(updateResponse.toString());
} catch (ResourceNotFoundException e) {
System.out.println("Domain not found. Please check the domain name.");
}
}
/**
* Deletes an Amazon OpenSearch Service domain. Deleting a domain can take
* several minutes.
*
* @param client
* The client to use for the requests to Amazon OpenSearch 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 OpenSearch 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 15-30 minutes
* to initialize, but can take longer depending on the configuration. 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 client to use for the requests to Amazon OpenSearch 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);
// Every 15 seconds, check whether the domain is processing.
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 OpenSearch Service has finished processing changes for your domain.");
System.out.println("Domain description response from Amazon OpenSearch Service:");
System.out.println(describeResponse.toString());
}
}
Python
この例では、OpenSearchService低レベル Python クライアントを使用して、AWS SDK for Python (Boto)からドメインの作成、その設定の更新、および削除を行います。
import boto3
import botocore
from botocore.config import Config
import time
# Build the client using the default credential configuration.
# You can use the CLI and run 'aws configure' to set access key, secret
# key, and default region.
my_config = Config(
# Optionally lets you specify a region other than your default.
region_name='us-west-2'
)
client = boto3.client('opensearch', config=my_config)
domainName = 'my-test-domain' # The name of the domain
def createDomain(client, domainName):
"""Creates an Amazon OpenSearch Service domain with the specified options."""
response = client.create_domain(
DomainName=domainName,
EngineVersion='OpenSearch_1.0',
ClusterConfig={
'InstanceType': 't2.small.search',
'InstanceCount': 5,
'DedicatedMasterEnabled': True,
'DedicatedMasterType': 't2.small.search',
'DedicatedMasterCount': 3
},
# Many instance types require EBS storage.
EBSOptions={
'EBSEnabled': True,
'VolumeType': 'gp2',
'VolumeSize': 10
},
AccessPolicies="{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"arn:aws:iam::123456789012:user/user-name\"]},\"Action\":[\"es:*\"],\"Resource\":\"arn:aws:es:us-west-2:123456789012:domain/my-test-domain/*\"}]}",
NodeToNodeEncryptionOptions={
'Enabled': True
}
)
print("Creating domain...")
print(response)
def updateDomain(client, domainName):
"""Updates the domain to use three data nodes instead of five."""
try:
response = client.update_domain_config(
DomainName=domainName,
ClusterConfig={
'InstanceCount': 3
}
)
print('Sending domain update request...')
print(response)
except botocore.exceptions.ClientError as error:
if error.response['Error']['Code'] == 'ResourceNotFoundException':
print('Domain not found. Please check the domain name.')
else:
raise error
def deleteDomain(client, domainName):
"""Deletes an OpenSearch Service domain. Deleting a domain can take several minutes."""
try:
response = client.delete_domain(
DomainName=domainName
)
print('Sending domain deletion request...')
print(response)
except botocore.exceptions.ClientError as error:
if error.response['Error']['Code'] == 'ResourceNotFoundException':
print('Domain not found. Please check the domain name.')
else:
raise error
def waitForDomainProcessing(client, domainName):
"""Waits for the domain to finish processing changes."""
try:
response = client.describe_domain(
DomainName=domainName
)
# Every 15 seconds, check whether the domain is processing.
while response["DomainStatus"]["Processing"] == True:
print('Domain still processing...')
time.sleep(15)
response = client.describe_domain(
DomainName=domainName)
# Once we exit the loop, the domain is available.
print('Amazon OpenSearch Service has finished processing changes for your domain.')
print('Domain description:')
print(response)
except botocore.exceptions.ClientError as error:
if error.response['Error']['Code'] == 'ResourceNotFoundException':
print('Domain not found. Please check the domain name.')
else:
raise error
def main():
"""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)
ノード
この例では、SDK for JavaScript in Node.js のバージョン 3 OpenSearch クライアントを使用して、ドメインの作成、その設定の更新、および削除を行います。
var {
OpenSearchClient,
CreateDomainCommand,
DescribeDomainCommand,
UpdateDomainConfigCommand,
DeleteDomainCommand
} = require("@aws-sdk/client-opensearch");
var sleep = require('sleep');
var client = new OpenSearchClient();
var domainName = 'my-test-domain'
// 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)
async function createDomain(client, domainName) {
// Creates an Amazon OpenSearch Service domain with the specified options.
var command = new CreateDomainCommand({
DomainName: domainName,
EngineVersion: 'OpenSearch_1.0',
ClusterConfig: {
'InstanceType': 't2.small.search',
'InstanceCount': 5,
'DedicatedMasterEnabled': 'True',
'DedicatedMasterType': 't2.small.search',
'DedicatedMasterCount': 3
},
EBSOptions:{
'EBSEnabled': 'True',
'VolumeType': 'gp2',
'VolumeSize': 10
},
AccessPolicies: "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"arn:aws:iam::123456789012:user/user-name\"]},\"Action\":[\"es:*\"],\"Resource\":\"arn:aws:es:us-east-1:123456789012:domain/my-test-domain/*\"}]}",
NodeToNodeEncryptionOptions:{
'Enabled': 'True'
}
});
const response = await client.send(command);
console.log("Creating domain...");
console.log(response);
}
async function updateDomain(client, domainName) {
// Updates the domain to use three data nodes instead of five.
var command = new UpdateDomainConfigCommand({
DomainName: domainName,
ClusterConfig: {
'InstanceCount': 3
}
});
const response = await client.send(command);
console.log('Sending domain update request...');
console.log(response);
}
async function deleteDomain(client, domainName) {
// Deletes an OpenSearch Service domain. Deleting a domain can take several minutes.
var command = new DeleteDomainCommand({
DomainName: domainName
});
const response = await client.send(command);
console.log('Sending domain deletion request...');
console.log(response);
}
async function waitForDomainProcessing(client, domainName) {
// Waits for the domain to finish processing changes.
try {
var command = new DescribeDomainCommand({
DomainName: domainName
});
var response = await client.send(command);
while (response.DomainStatus.Processing == true) {
console.log('Domain still processing...')
await sleep(15000) // Wait for 15 seconds, then check the status again
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
var response = await client.send(command);
}
// Once we exit the loop, the domain is available.
console.log('Amazon OpenSearch Service has finished processing changes for your domain.');
console.log('Domain description:');
console.log(response);
} catch (error) {
if (error.name === 'ResourceNotFoundException') {
console.log('Domain not found. Please check the domain name.');
}
};
}