DynamoDB 和 TableNameOverride 带前缀

2022-01-15 00:00:00 amazon-dynamodb java

我正在测试 DynamoDB 表,并希望使用前缀dev_"为 prod 和 dev 环境设置不同的表名以进行开发.

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

我做了这个测试来打印表名:

I made 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());

这打印:name=null prefix=dev_
这里的名字怎么是空的?

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

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

这打印:name=test prefix=null

*如何让表名成为dev_test"?*

我想稍后使用它为开发模式下的所有表获取dev_"前缀,如下所示:

I want to use this later to get a "dev_" prefix for all tables in development mode like this:

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

或者是否有另一种解决方案来分隔 dev 表和 prod 表?
我首先想到将它们放在不同的区域,但不确定.

Or is there another solution to separate between dev and prod tables?
I first thought of putting them in separate regions but not sure about this.

也可以这样用:

mapper.save(ck, new DynamoDBMapperConfig(new TableNameOverride((isDev ? "dev_" : "") + annotation.tableName())));

推荐答案

withTableNamePrefix 是一个静态方法.因此,这一行使用字符串test"创建 TableNameOverride 的新实例,然后通过调用静态 withTableNamePrefix 方法将该实例丢弃:

withTableNamePrefix is a static method. So this line is creating a new instance of TableNameOverride with the String "test", and then throwing that instance away by using it to call the static withTableNamePrefix method:

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

要回答将测试与生产分开的更深层次的问题,我建议完全拥有 2 个单独的 AWS 账户,一个用于开发,一个用于生产.这是您可以做到的唯一方法:

To answer the deeper question of separating test from prod, I would recommend having 2 separate AWS Accounts entirely, one for dev and one for prod. This is the only way you can:

  • 单独查看帐单
  • 确保您绝不在产品和测试系统之间泄露数据
  • 在 dev 表上进行高扩展会阻止您将 prod 表扩展得更高
  • See billing separately
  • Ensure you never leak data between prod and test systems
  • Have high scaling on a dev table prevent you from scaling a prod table higher

相关文章