시작하기(AWS SDK for Java) - Amazon Kendra

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

시작하기(AWS SDK for Java)

다음 프로그램은 Java 프로그램에서 Amazon Kendra 를 사용하는 예제입니다. 프로그램이 실행하는 작업은 다음과 같습니다.

  1. CreateIndex 작업을 사용하여 새 인덱스를 생성합니다.

  2. 인덱스 생성이 완료될 때까지 기다립니다. 작업을 사용하여 인덱스의 상태를 DescribeIndex 모니터링합니다.

  3. 인덱스가 활성화되면 CreateDataSource 작업을 사용하여 데이터 소스를 생성합니다.

  4. 데이터 소스 생성이 완료될 때까지 기다립니다. 작업을 사용하여 데이터 소스의 상태를 DescribeDataSource 모니터링합니다.

  5. 데이터 소스가 활성화되면 StartDataSourceSyncJob 작업을 사용하여 인덱스를 데이터 소스의 콘텐츠와 동기화합니다.

package com.amazonaws.kendra; import java.util.concurrent.TimeUnit; import software.amazon.awssdk.services.kendra.KendraClient; import software.amazon.awssdk.services.kendra.model.CreateDataSourceRequest; import software.amazon.awssdk.services.kendra.model.CreateDataSourceResponse; import software.amazon.awssdk.services.kendra.model.CreateIndexRequest; import software.amazon.awssdk.services.kendra.model.CreateIndexResponse; import software.amazon.awssdk.services.kendra.model.DataSourceConfiguration; import software.amazon.awssdk.services.kendra.model.DataSourceStatus; import software.amazon.awssdk.services.kendra.model.DataSourceSyncJob; import software.amazon.awssdk.services.kendra.model.DataSourceSyncJobStatus; import software.amazon.awssdk.services.kendra.model.DataSourceType; import software.amazon.awssdk.services.kendra.model.DescribeDataSourceRequest; import software.amazon.awssdk.services.kendra.model.DescribeDataSourceResponse; import software.amazon.awssdk.services.kendra.model.DescribeIndexRequest; import software.amazon.awssdk.services.kendra.model.DescribeIndexResponse; import software.amazon.awssdk.services.kendra.model.IndexStatus; import software.amazon.awssdk.services.kendra.model.ListDataSourceSyncJobsRequest; import software.amazon.awssdk.services.kendra.model.ListDataSourceSyncJobsResponse; import software.amazon.awssdk.services.kendra.model.S3DataSourceConfiguration; import software.amazon.awssdk.services.kendra.model.StartDataSourceSyncJobRequest; import software.amazon.awssdk.services.kendra.model.StartDataSourceSyncJobResponse; public class CreateIndexAndDataSourceExample { public static void main(String[] args) throws InterruptedException { System.out.println("Create an index"); String indexDescription = "Getting started index for Kendra"; String indexName = "java-getting-started-index"; String indexRoleArn = "arn:aws:iam::<your AWS account ID>:role/<name of an IAM role>"; System.out.println(String.format("Creating an index named %s", indexName)); KendraClient kendra = KendraClient.builder().build(); CreateIndexRequest createIndexRequest = CreateIndexRequest .builder() .description(indexDescription) .name(indexName) .roleArn(indexRoleArn) .build(); CreateIndexResponse createIndexResponse = kendra.createIndex(createIndexRequest); System.out.println(String.format("Index response %s", createIndexResponse)); String indexId = createIndexResponse.id(); System.out.println(String.format("Waiting until the index with index ID %s is created", indexId)); while (true) { DescribeIndexRequest describeIndexRequest = DescribeIndexRequest.builder().id(indexId).build(); DescribeIndexResponse describeIndexResponse = kendra.describeIndex(describeIndexRequest); IndexStatus status = describeIndexResponse.status(); if (status != IndexStatus.CREATING) { break; } TimeUnit.SECONDS.sleep(60); } System.out.println("Creating an S3 data source"); String dataSourceName = "java-getting-started-data-source"; String dataSourceDescription = "Getting started data source"; String s3BucketName = "amzn-s3-demo-bucket"; String dataSourceRoleArn = "arn:aws:iam::<your AWS account ID>:role/<name of an IAM role>"; CreateDataSourceRequest createDataSourceRequest = CreateDataSourceRequest .builder() .indexId(indexId) .name(dataSourceName) .description(dataSourceDescription) .roleArn(dataSourceRoleArn) .type(DataSourceType.S3) .configuration( DataSourceConfiguration .builder() .s3Configuration( S3DataSourceConfiguration .builder() .bucketName(s3BucketName) .build() ).build() ).build(); CreateDataSourceResponse createDataSourceResponse = kendra.createDataSource(createDataSourceRequest); System.out.println(String.format("Response of creating data source: %s", createDataSourceResponse)); String dataSourceId = createDataSourceResponse.id(); System.out.println(String.format("Waiting for Kendra to create the data source %s", dataSourceId)); DescribeDataSourceRequest describeDataSourceRequest = DescribeDataSourceRequest .builder() .indexId(indexId) .id(dataSourceId) .build(); while (true) { DescribeDataSourceResponse describeDataSourceResponse = kendra.describeDataSource(describeDataSourceRequest); DataSourceStatus status = describeDataSourceResponse.status(); System.out.println(String.format("Creating data source. Status: %s", status)); if (status != DataSourceStatus.CREATING) { break; } TimeUnit.SECONDS.sleep(60); } System.out.println(String.format("Synchronize the data source %s", dataSourceId)); StartDataSourceSyncJobRequest startDataSourceSyncJobRequest = StartDataSourceSyncJobRequest .builder() .indexId(indexId) .id(dataSourceId) .build(); StartDataSourceSyncJobResponse startDataSourceSyncJobResponse = kendra.startDataSourceSyncJob(startDataSourceSyncJobRequest); System.out.println(String.format("Waiting for the data source to sync with the index %s for execution ID %s", indexId, startDataSourceSyncJobResponse.executionId())); // For this particular list, there should be just one job ListDataSourceSyncJobsRequest listDataSourceSyncJobsRequest = ListDataSourceSyncJobsRequest .builder() .indexId(indexId) .id(dataSourceId) .build(); while (true) { ListDataSourceSyncJobsResponse listDataSourceSyncJobsResponse = kendra.listDataSourceSyncJobs(listDataSourceSyncJobsRequest); DataSourceSyncJob job = listDataSourceSyncJobsResponse.history().get(0); System.out.println(String.format("Syncing data source. Status: %s", job.status())); if (job.status() != DataSourceSyncJobStatus.SYNCING) { break; } TimeUnit.SECONDS.sleep(60); } System.out.println("Index setup is complete"); } }