Getting started (AWS SDK for Java) - Amazon Kendra

Getting started (AWS SDK for Java)

The following program is an example of using Amazon Kendra in a Java program. The program performs the following actions:

  1. Creates a new index using the CreateIndex operation.

  2. Waits for index creation to complete. It uses the DescribeIndex operation to monitor the status of the index.

  3. Once the index is active, it creates a data source using the CreateDataSource operation.

  4. Waits for data source creation to complete. It uses the DescribeDataSource operation to monitor the status of the data source.

  5. When the data source is active, it synchronizes the index with the contents of the data source using the StartDataSourceSyncJob operation.

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 = "an-aws-kendra-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"); } }