gpt4 book ai didi

java - 如何使用 @DynamoDbTable 注释动态传递 aws dynamodb 表名称

转载 作者:行者123 更新时间:2023-12-03 08:37:45 27 4
gpt4 key购买 nike

我创建了一个 java 服务来从 dynamodb 获取详细信息,它工作正常,但我的挑战是我将表名称硬编码到类文件 @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

我无法找出问题所在。如果我在类文件中使用表名,则工作正常,如果我尝试使用代码替换表名,则会出现错误。

最佳答案

DynamoDBMapper 需要 AmazonDynamoDB 的任何实现,但不需要 DynamoDB 类。

public DynamoDBMapper(
final AmazonDynamoDB dynamoDB,
final DynamoDBMapperConfig config) {

您只需在服务中注入(inject) 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 类,但您无法使用 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 - 如何使用 @DynamoDbTable 注释动态传递 aws dynamodb 表名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63455756/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com