如何使用@DynamoDbTable 注释动态传递 aws dynamodb table 名称
How to pass aws dynamodb table name dynamically using @DynamoDbTable annotation
我创建了一个 java 服务来从 dynamodb 获取详细信息,它工作正常但我的挑战是我将 table 名称硬编码到 class 文件 @DynamoDbTable 注释以便在不同的环境中工作,我觉得这不是正确的方法。我在下面给出了我的代码,有人可以帮我解决这个问题吗?
代码示例
public class DynamodbService {
private DynamoDB client;
private DynamoDBMapper objectMapper;
/**
*
* @param client
* @param objectMapper
*/
@Autowired
public DynamodbService(DynamoDB client, DynamoDBMapper objectMapper) {
this.client = client;
this.objectMapper = objectMapper;
}
public List<Dynamodb> findAll() throws Exception {
DynamoDBMapperConfig mapperConfig = new DynamoDBMapperConfig.Builder()
.withTableNameOverride(DynamoDBMapperConfig.TableNameOverride.withTableNameReplacement(""))
.build();
DynamoDBMapper mapper = new DynamoDBMapper(client, mapperConfig);
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
List<Dynamodb> scanResult = objectMapper.scan(Dynamodb.class, scanExpression);
return scanResult;
}
}
我的 DynamoDB 配置
@Configuration
public class DynamoDBconfig {
/**
*
*/
@Value("${amazon.dynamodb.accesskey}")
private String awsAccessKey;
/**
*
*/
@Value("${amazon.dynamodb.secretkey}")
private String awsSecretKey;
@Bean
public AWSCredentials amazonAWSCredentials() {
return new BasicAWSCredentials(aes.getDecryptedvalue(awsAccessKey), aes.getDecryptedvalue(awsSecretKey));
}
/**
*
* @return
*/
@Bean
public DynamoDBMapperConfig dynamoDBMapperConfig() {
return DynamoDBMapperConfig.DEFAULT;
}
/**
*
* @param amazonDynamoDB
* @param config
* @return
*/
@Bean
public DynamoDBMapper dynamoDBMapper(AmazonDynamoDB amazonDynamoDB, DynamoDBMapperConfig config) {
return new DynamoDBMapper(amazonDynamoDB, config);
}
/**
*
* @return
*/
@Bean
public AmazonDynamoDB amazonDynamoDB() {
return AmazonDynamoDBClientBuilder.standard().withCredentials(amazonAWSCredentialsProvider())
.withRegion(Regions.US_EAST_2).build();
}
/**
*
* @return
*/
@Bean
public DynamoDB dynamoDB() {
return new DynamoDB(amazonDynamoDB());
}
/**
*
* @return
*/
public AWSCredentialsProvider amazonAWSCredentialsProvider() {
return new AWSStaticCredentialsProvider(amazonAWSCredentials());
}
}
我的 dynamodb 服务出错
The constructor DynamoDBMapper(DynamoDB, DynamoDBMapperConfig) is undefined
我无法找出问题所在。如果我在 class 文件中使用 table 名称它工作正常如果我尝试使用代码替换 table 名称它最终会出错。
DynamoDBMapper
期望 AmazonDynamoDB 的任何实现,但不是 DynamoDB class。
public DynamoDBMapper(
final AmazonDynamoDB dynamoDB,
final DynamoDBMapperConfig config) {
您只需在服务中注入 DynamoDBMapper。
@Bean
public DynamoDBMapper dynamoDBMapper(AmazonDynamoDB amazonDynamoDB) {
DynamoDBMapperConfig config = new DynamoDBMapperConfig.Builder().withTableNameOverride(TableNameOverride.withTableNameReplacement(tableName))
.build();
return new DynamoDBMapper(amazonDynamoDB, config);
}
其他选项可以使用 com.amazonaws.services.dynamodbv2.document.DynamoDB
class,但您无法使用 DynamoDBScanExpression
,您需要重写代码。
String desiredTabledName = "table-name";
Table table = dynamoDB.getTable(desiredTabledName);
You have multiple options.
- table.scan() and table query.
- Index index = table.getIndex(indexName);
index.scan() or index.query()
- You could pass `QuerySpec` and `ScanSpec` to all above.
我创建了一个 java 服务来从 dynamodb 获取详细信息,它工作正常但我的挑战是我将 table 名称硬编码到 class 文件 @DynamoDbTable 注释以便在不同的环境中工作,我觉得这不是正确的方法。我在下面给出了我的代码,有人可以帮我解决这个问题吗?
代码示例
public class DynamodbService {
private DynamoDB client;
private DynamoDBMapper objectMapper;
/**
*
* @param client
* @param objectMapper
*/
@Autowired
public DynamodbService(DynamoDB client, DynamoDBMapper objectMapper) {
this.client = client;
this.objectMapper = objectMapper;
}
public List<Dynamodb> findAll() throws Exception {
DynamoDBMapperConfig mapperConfig = new DynamoDBMapperConfig.Builder()
.withTableNameOverride(DynamoDBMapperConfig.TableNameOverride.withTableNameReplacement(""))
.build();
DynamoDBMapper mapper = new DynamoDBMapper(client, mapperConfig);
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
List<Dynamodb> scanResult = objectMapper.scan(Dynamodb.class, scanExpression);
return scanResult;
}
}
我的 DynamoDB 配置
@Configuration
public class DynamoDBconfig {
/**
*
*/
@Value("${amazon.dynamodb.accesskey}")
private String awsAccessKey;
/**
*
*/
@Value("${amazon.dynamodb.secretkey}")
private String awsSecretKey;
@Bean
public AWSCredentials amazonAWSCredentials() {
return new BasicAWSCredentials(aes.getDecryptedvalue(awsAccessKey), aes.getDecryptedvalue(awsSecretKey));
}
/**
*
* @return
*/
@Bean
public DynamoDBMapperConfig dynamoDBMapperConfig() {
return DynamoDBMapperConfig.DEFAULT;
}
/**
*
* @param amazonDynamoDB
* @param config
* @return
*/
@Bean
public DynamoDBMapper dynamoDBMapper(AmazonDynamoDB amazonDynamoDB, DynamoDBMapperConfig config) {
return new DynamoDBMapper(amazonDynamoDB, config);
}
/**
*
* @return
*/
@Bean
public AmazonDynamoDB amazonDynamoDB() {
return AmazonDynamoDBClientBuilder.standard().withCredentials(amazonAWSCredentialsProvider())
.withRegion(Regions.US_EAST_2).build();
}
/**
*
* @return
*/
@Bean
public DynamoDB dynamoDB() {
return new DynamoDB(amazonDynamoDB());
}
/**
*
* @return
*/
public AWSCredentialsProvider amazonAWSCredentialsProvider() {
return new AWSStaticCredentialsProvider(amazonAWSCredentials());
}
}
我的 dynamodb 服务出错
The constructor DynamoDBMapper(DynamoDB, DynamoDBMapperConfig) is undefined
我无法找出问题所在。如果我在 class 文件中使用 table 名称它工作正常如果我尝试使用代码替换 table 名称它最终会出错。
DynamoDBMapper
期望 AmazonDynamoDB 的任何实现,但不是 DynamoDB class。
public DynamoDBMapper(
final AmazonDynamoDB dynamoDB,
final DynamoDBMapperConfig config) {
您只需在服务中注入 DynamoDBMapper。
@Bean
public DynamoDBMapper dynamoDBMapper(AmazonDynamoDB amazonDynamoDB) {
DynamoDBMapperConfig config = new DynamoDBMapperConfig.Builder().withTableNameOverride(TableNameOverride.withTableNameReplacement(tableName))
.build();
return new DynamoDBMapper(amazonDynamoDB, config);
}
其他选项可以使用 com.amazonaws.services.dynamodbv2.document.DynamoDB
class,但您无法使用 DynamoDBScanExpression
,您需要重写代码。
String desiredTabledName = "table-name";
Table table = dynamoDB.getTable(desiredTabledName);
You have multiple options.
- table.scan() and table query.
- Index index = table.getIndex(indexName);
index.scan() or index.query()
- You could pass `QuerySpec` and `ScanSpec` to all above.