Ele*_*gie 5 amazon-dynamodb aws-sdk-java
我有一个 Java Spring 应用程序,其中使用 DynamoDB,到目前为止还没有事务。然而,我最近遇到了一个新的用例,它需要将相关对象持久化在一起。我是这个主题的新手,因此阅读过有关 DynamoDB 事务的内容,但找不到以正确的面向对象方式使用 API 的方法。我缺少什么?
现在,当我需要更新对象时,我按如下方式进行操作,构建一个UpdateItemRequest:
Map<String, AttributeValueUpdate> updates = new HashMap<>();
// fill updates
Map<String, ExpectedAttributeValue> expected = new HashMap<>();
// fill expectations
UpdateItemRequest request = new UpdateItemRequest()
.withTableName(TABLE_NAME)
.withKey(key)
.withAttributeUpdates(updates)
.withExpected(expected);
dynamoDBClient.updateItem(request);
Run Code Online (Sandbox Code Playgroud)
但是,构建交易的文档建议使用以下语法:
Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":new_status", new AttributeValue("SOLD"));
expressionAttributeValues.put(":expected_status", new AttributeValue("IN_STOCK"));
Update markItemSold = new Update()
.withTableName(PRODUCT_TABLE_NAME)
.withKey(productItemKey)
.withUpdateExpression("SET ProductStatus = :new_status")
.withExpressionAttributeValues(expressionAttributeValues)
.withConditionExpression("ProductStatus = :expected_status")
.withReturnValuesOnConditionCheckFailure(ReturnValuesOnConditionCheckFailure.ALL_OLD);
Run Code Online (Sandbox Code Playgroud)
如果我要遵循文档,那么我需要构建一个update expression- 但我坚持的内容可能非常复杂,这将需要一些繁重的字符串操作,这并不令人满意(要制作的解析器会很慢并且容易出错)。
update expression有没有办法从 a获取这个UpdateItemRequest?或者有什么推荐的方法来构建如此复杂的表达式,例如作为某种东西的序列化形式?或者更好的是,某种面向对象的方式来使用事务,传递更新对象的映射而不是一个大字符串?
谢谢。
| 归档时间: |
|
| 查看次数: |
721 次 |
| 最近记录: |