2

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.

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

the dynamoDbClient is of type AmazonDynamoDB and not DynamoDB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.