简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM