DynamoDB - GlobalSecondaryIndex for table conflicts with @DynamoDBIndexHashKey

Viewed 247

I have a DynamoDB table:

@DynamoDBTable(tableName = "my-table")
data class SomeJobEntity(
  @DynamoDBHashKey
  var jobId: String? = null,
  @DynamoDBIndexHashKey(globalSecondaryIndexName = "jobStatus_idx")
  var jobStatus: String? = null
)

The field 'jobStatus' is annotated with @DynamoDBIndexHashKey, which is useful for several reasons (e.g. when reading the code you understand this field has an index and you can query it safely).

In an integration test this table is created with:

val createTableRequest = mapper.generateCreateTableRequest(SomeJobEntity::class.java)
                .withTableName(tableName)
                .withAttributeDefinitions(
                        stringAttribute("jobStatus")
                )
                .withGlobalSecondaryIndexes(
                        createGlobalSecondaryIndex("jobStatus_idx", "jobStatus")
                )
                .withProvisionedThroughput(defaultProvisionedThroughput())

Both the table and in the createGlobalSecondaryIndex() function, a provisioned throughput is defined:

ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L)

However, defining both this annotation and manually creating the secondary index in the integration test, causes problems when executing the integration test: com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: Cannot have two attributes with the same name

So one solution, which works, is to remove the @DynamoDBIndexHashKey annotation, and only one secondary index is created in the integration test.

On the other hand, if we really want to use this annotation, and not create the secondary index in the createTableRequest, then it will create the index without the provisioned throughput and you get the error: com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: No provisioned throughput specified for the global secondary index

So my question is: how can you use the annotation @DynamoDBIndexHashKey in a way that successfully creates a secondary index for an integration test?

0 Answers
Related