Conceitos básicos (AWS SDK for Java) - Amazon Kendra

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Conceitos básicos (AWS SDK for Java)

O programa a seguir é um exemplo de uso Amazon Kendra em um programa Java. O programa realiza as seguintes ações:

  1. Cria um novo índice usando a CreateIndexoperação.

  2. Aguardar a conclusão da criação do índice. Ele usa a DescribeIndexoperação para monitorar o status do índice.

  3. Quando o índice está ativo, ele cria uma fonte de dados usando a CreateDataSourceoperação.

  4. Aguardar a conclusão da criação da fonte de dados. Ele usa a DescribeDataSourceoperação para monitorar o status da fonte de dados.

  5. Quando a fonte de dados está ativa, ela sincroniza o índice com o conteúdo da fonte de dados usando a StartDataSourceSyncJoboperação.

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"); } }