기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
이 섹션에는 AWS SDK를 사용하여 Amazon OpenSearch Service 구성 API와 상호 작용하는 방법의 예제가 나와 있습니다. 이러한 코드 샘플은 OpenSearch Service 도메인을 생성, 업데이트, 삭제하는 방법을 보여줍니다.
Java
이 단원에는 AWS SDK for Java 버전 1 및 2에 대한 예시가 포함되어 있습니다.
이 예시는 AWS SDK for Java의 버전 2에서 OpenSearchClientBuilderwaitForDomainProcessing
에 대한 호출을 제거하여 (그리고 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());
}
}
Python
이 예시는 AWS SDK for Python (Boto)의 OpenSearchService
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)
노드
이 예시는 Node.js OpenSearch client의 JavaScript용 SDK 버전 3을 사용하여 도메인을 생성하고, 도메인 구성을 업데이트하고, 도메인을 삭제합니다.
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.');
}
};
}