1
votes

I'm working on an Android application which has a purpose to create a DynamoDB table every week. I'm getting the current week number:

Time time = new Time(); 
time.setToNow(); 
curweekNumb = time.getWeekNumber();

So I want to modify the table name as:

Constants.TEST_TABLE_NAME = "Week_" + currentweekNumb; 

Whenever the currentweekNumb value changes.

The Constants class:

public class Constants {
    public static String TEST_TABLE_NAME = "Week_1";
}

The main issue here, is that I'm getting an error indicating that the value for annotation attribute DynamoDBTable.tableName must be a constant expression. So it requires a "final" String value as a tableName. In this case, I'm not able to change that value anywhere in the application. Is it impossible to dynamically change a table name in the application ?.

@DynamoDBTable(tableName = Constants.TEST_TABLE_NAME) <--- The error is on this line
public static class UserPreference {
    private int Tid;
    private String Mandag;     

    @DynamoDBHashKey(attributeName = "Tid")
    public int getTid() {
        return Tid;
    }

    public void setTid(int Tid) {
        this.Tid = Tid;
    }

    @DynamoDBAttribute(attributeName = "Mandag")
    public String getMandag() {
        return Mandag;
    }

    public void setMandag(String Mandag) {
        this.Mandag = Mandag;
    }       
}
2
is "Constants.TEST_TABLE_NAME" final? - Lesto
When I change the String to final, the error goes away, but in this case I won't be able to change the value, and this is what I want. - Osman Esen

2 Answers

1
votes

While @lesto's answer is valid i wouldn't recommend making a ew table every week. In long run it may not be scalable. DynamoDB has a limit of 256 table per account per region so eventually you are going to run into it. See https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html#limits-tables

I would suggest you create a partition key with the week as a partition.

0
votes

Annotation are hadled at compile time, so you can't make them dynamic. But you can bypass them;

UserPreference userTable = ..
String tableName = .. // Get table name
dynamoMapper.save(userTable, new DynamoDBMapperConfig(new TableNameOverride(tableName)));

you may even use a list of table but you will have to use batchWrite() isntead of save()