Reputation: 607
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
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