Ayush Shukla
Ayush Shukla

Reputation: 607

Create DynamoDB Global Secondary Index after table creation in Java

Is it possible to create GSI on an existing table programmatically from java? I know that its possible while creating a new table using

dynamoDB.createTable(new CreateTableRequest().withGlobalSecondaryIndexes(index));

I also know that it is possible to create index after creating table from web.

Upvotes: 2

Views: 1017

Answers (1)

David Wright
David Wright

Reputation: 171

You will need to use the GlobalSecondaryIndexUpdate way of doing this, as described here: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GlobalSecondaryIndexUpdate.html

It should look something like this

CreateGlobalSecondaryIndexAction action = CreateGlobalSecondaryIndexAction
                .builder()
                .indexName("index-name")
                .keySchema(theSchema)
                .build();
GlobalSecondaryIndexUpdate index = GlobalSecondaryIndexUpdate
                .builder()
                .create(action)
                .build();
UpdateTableRequest request = UpdateTableRequest
                .builder()
                .tableName("table-name")
                .globalSecondaryIndexUpdates(index)
                .build();
dynamoDbClient.updateTable(request);

Upvotes: 4

Related Questions