gpt4 book ai didi

java - 用于条件保存的 DynamoDBMapper

转载 作者:搜寻专家 更新时间:2023-10-30 19:53:32 24 4
gpt4 key购买 nike

我正在使用 DynamoDBMapper,当且仅当散列键和范围键组合不存在时,我想有条件地保存。我知道有一些方法可以使用 UUID 来减少碰撞的可能性,但我想通过使用条件保存来保护自己。

我遇到了 this article使用 DynamoDBSaveExpression但是我无法指定条件是“hashkey AND rangekey”不存在。 API指定了一个 withConditionalOperator 方法,但我在类里面看不到它。我正在使用来自 here 的最新 aws java sdk。 .

关于如何有条件地保存有什么建议吗?或者我可能做错了什么?

最佳答案

DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression();
Map<String, ExpectedAttributeValue> expectedAttributes =
ImmutableMap.<String, ExpectedAttributeValue>builder()
.put("hashKey", new ExpectedAttributeValue(false))
.put("rangeKey", new ExpectedAttributeValue(false))
.build();
saveExpression.setExpected(expectedAttributes);
saveExpression.setConditionalOperator(ConditionalOperator.AND);
try {
dynamoDBMapper.save(objectToSave, saveExpression);
} catch (ConditionalCheckFailedException e) {
//Handle conditional check
}

这使用了 public ExpectedAttributeValue(Boolean exists) 构造函数,它只是在内部调用 setExists

关于java - 用于条件保存的 DynamoDBMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28081401/

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