I'm using the Java lib https://github.com/boostchicken/spring-data-dynamodb to handle dynamoDB connection with spring boot.
I have a table with the attribute "id" as the primary key (only HASH key there is no RANK key in the primary key) and also "type" as a GSI (only HASH key).
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import java.io.Serializable;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@DynamoDBTable(tableName = "accounts")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account implements Serializable {
private static final long serialVersionUID = -6627846284567286502L;
@DynamoDBHashKey
@DynamoDBAutoGeneratedKey
private UUID id;
@DynamoDBIndexHashKey(globalSecondaryIndexName = "GSI-type")
private String type;
private AccountAttributes attributes;
@DynamoDBDocument
@Data
@AllArgsConstructor
@NoArgsConstructor
static public class AccountAttributes implements Serializable {
private static final long serialVersionUID = -1464248632731386692L;
private String name;
}
}
the table name changes in the deployment to be "accounts-staging" or "accounts-production"
so I configure the bean "DynamoDBMapperConfig" with overriding the table name in the run time
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig.TableNameOverride;
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
@EnableDynamoDBRepositories(basePackages = "com.project.repositories")
public class DynamoDBConfig {
private final EnvironmentVariables env;
/**
* Constructor to initialize the data field object.
*
* @param env the environment variables object
*/
@Autowired
public DynamoDBConfig(EnvironmentVariables env) {
this.env = env;
}
@Bean
public AmazonDynamoDB amazonDynamoDB() {
AmazonDynamoDBClientBuilder dynamoDBClientBuilder = AmazonDynamoDBClientBuilder.standard();
dynamoDBClientBuilder.withRegion("us-east-1");
return dynamoDBClientBuilder.build();
}
@Bean
@Primary
public DynamoDBMapperConfig dynamoDBMapperConfig() {
return DynamoDBMapperConfig.builder()
.withTableNameOverride(TableNameOverride.withTableNameReplacement(
"accounts-" + env.getEnvironment()))
.withBatchLoadRetryStrategy(DynamoDBMapperConfig.DEFAULT.getBatchLoadRetryStrategy())
.withBatchWriteRetryStrategy(DynamoDBMapperConfig.DEFAULT.getBatchWriteRetryStrategy())
.withConsistentReads(DynamoDBMapperConfig.DEFAULT.getConsistentReads())
.withConversionSchema(DynamoDBMapperConfig.DEFAULT.getConversionSchema())
.withObjectTableNameResolver(DynamoDBMapperConfig.DEFAULT.getObjectTableNameResolver())
.withPaginationLoadingStrategy(DynamoDBMapperConfig.DEFAULT.getPaginationLoadingStrategy())
.withRequestMetricCollector(DynamoDBMapperConfig.DEFAULT.getRequestMetricCollector())
.withSaveBehavior(DynamoDBMapperConfig.DEFAULT.getSaveBehavior())
.withTypeConverterFactory(DynamoDBMapperConfig.DEFAULT.getTypeConverterFactory())
.withObjectTableNameResolver(DynamoDBMapperConfig.DEFAULT.getObjectTableNameResolver())
.build();
}
}
When I try to get an account by id, it works fine but when I try to get account by type, I got this exception
Requested resource not found (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ResourceNotFoundException; Request ID: 565NMFN0IBNHV95JNVBJART4FFVV4KQNSO5AEMVJF66Q9ASUAAJG)
I tried to change (in the Account.java class) the table name in the annotation "DynamoDBTable" with the right name, it works fine.
So it seems like with the GSI the bean "DynamoDBMapperConfig" didn't override the table name.
any help thanks