DynamoDB and TableNameOverride prefixed

I am testing DynamoDB tables and want to configure different table names for the prod and dev environment using the dev_ prefix for development.

I did this test to print the table name:

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig.TableNameOverride; TableNameOverride tbl = new TableNameOverride("test").withTableNamePrefix("dev_"); System.out.println("name=" + tbl.getTableName() + " prefix=" + tbl.getTableNamePrefix()); 

Fingerprints: name=null prefix=dev_
How come the name is null here?

 TableNameOverride tbl = new TableNameOverride("test");//.withTableNamePrefix("dev_"); System.out.println("name=" + tbl.getTableName() + " prefix=" + tbl.getTableNamePrefix()); 

Fingerprints: name=test prefix=null

* How to get the table name as "dev_test"? *

I want to use this later to get the prefix "dev_" for all tables in design mode, for example:

 DynamoDBTable annotation = (DynamoDBTable) myclass.getClass().getAnnotation(DynamoDBTable.class); TableNameOverride tbl = new TableNameOverride(annotation.tableName()).withTableNamePrefix("dev_"); 

Or is there another solution for sharing between dev and prod tables?
At first I thought about putting them in separate regions, but I'm not sure about that.

You can also use this:

 mapper.save(ck, new DynamoDBMapperConfig(new TableNameOverride((isDev ? "dev_" : "") + annotation.tableName()))); 
+7
java amazon-dynamodb
source share
2 answers

withTableNamePrefix is a static method. Thus, this row creates a new instance of TableNameOverride using String "test", and then deletes this instance, using it to call the static method withTableNamePrefix :

 TableNameOverride tbl = new TableNameOverride("test").withTableNamePrefix("dev_"); 

To answer a deeper question about the separation of the test from prod, I would recommend having 2 separate AWS accounts in full, one for dev and one for prod. This is the only way:

  • See billing section separately.
  • Make sure you never leak data between prod and test systems.
  • High scaling in the dev table prevents prod from scaling.
+5
source share

I faced the same situation and struggled with myself a couple of days to get this job done.

Just in case you are using DynamoDB + Spring, this is what worked for me:

POJO Class:

 @DynamoDBTable(tableName = "APP-ACCESSKEY") public class AccessKey { @NotBlank @Size(min = 1, max = 36) private String accessToken; @NotNull @Size(min = 3, max = 15) private String userName; private Date dateInsertion; public AccessKey() { // ... All POJO stuff } 

Spring Configuration:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- Amazon Credentials --> <bean id="tableNameOverride" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod" value="com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig.TableNameOverride.withTableNamePrefix"/> <property name="arguments" value="DES-" /> </bean> <bean id="dynamoDBMapperConfig" class="com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig"> <constructor-arg index="0" ref="tableNameOverride" /> </bean> <bean id="BasicAWSCredentials" class="com.amazonaws.auth.BasicAWSCredentials"> <constructor-arg index="0" value="${amazon.accessKey}" /> <constructor-arg index="1" value="${amazon.secretKey}" /> </bean> <bean id="amazonDynamoDBClient" class="com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient"> <constructor-arg index="0" ref="BasicAWSCredentials" /> <property name="endpoint" value="http://dynamodb.us-west-2.amazonaws.com" /> </bean> <bean id="dynamoDBMapper" class="com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper"> <constructor-arg index="0" ref="amazonDynamoDBClient" /> <constructor-arg index="1" ref="dynamoDBMapperConfig" /> </bean> </beans> 

Explanation:

Considering that my AccessKey points to the APP-ACCESSKEY table on AWS DynamodDB, it turns out that after it starts, your application will start pointing to DES-APP-ACCESSKEY.

Hope this helps someone who is faced with a similar situation.

Greetings

+7
source share

All Articles