Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create table with Global secondary index using DynamoMapper and class Annotation

I am currently using Java dynamoMapper to create and query the table. When trying to create a table with a global secondary index, I get the following error

No provisioned throughput specified for the global secondary index 

My java class representing the table has this attribute for the global secondary index.

@DynamoDBIndexHashKey(globalSecondaryIndexName="sender")
    public String getSender() {
    return sender;
}

Class to create table looks like this

public boolean createTable() {
try {
DynamoDBMapper mapper = new DynamoDBMapper(client);
CreateTableRequest tableRequest =     mapper.generateCreateTableRequest(entityClass); // 1
tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1000L, 1500L)); // 2
client.createTable(tableRequest); // 3

    } catch (Error e) {
        e.printStackTrace();
        return false;

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

I have searched the Amazon site for additional annotations and configuration but nothing came up for DynamoMapper. Is there anyway to do this using ORM or will i have to manually create by using a lower level API?

like image 383
Bryan Benauro Avatar asked Apr 23 '16 22:04

Bryan Benauro


People also ask

Can I add global secondary index to existing table?

To add a global secondary index to an existing table, use the UpdateTable operation with the GlobalSecondaryIndexUpdates parameter. You must provide the following: An index name. The name must be unique among all the indexes on the table.

What is global secondary index in DynamoDB?

DynamoDB supports two types of secondary indexes: Global secondary index — An index with a partition key and a sort key that can be different from those on the base table. A global secondary index is considered "global" because queries on the index can span all of the data in the base table, across all partitions.


2 Answers

You need to set the provisioned throughput on each secondary index table that will be generated as well.

tableRequest.getGlobalSecondaryIndexes().get(0).setProvisionedThroughput(new ProvisionedThroughput(10l, 10l));
like image 190
Jeremy Avatar answered Oct 14 '22 14:10

Jeremy


Extension to @Jeremy's answer.

If you have more than one GSI, then you may do it like below. Also you don't need to create ProvisionedThroughput object all the time if they are of same values.

 final ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(5L, 5L);
 createRequest.setProvisionedThroughput(provisionedThroughput);
 createRequest.getGlobalSecondaryIndexes().forEach(v -> v.setProvisionedThroughput(provisionedThroughput));
like image 40
Mohammad Adnan Avatar answered Oct 14 '22 13:10

Mohammad Adnan