我有一个 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);
但是,构建事务的文档建议使用以下语法:
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);
如果我要遵循文档,那么我需要构建一个update expression
- 但我坚持的内容可能非常复杂,这将需要一些繁重的字符串操作,这并不令人满意(解析器会很慢并且容易出错)。
有没有办法update expression
从一个获得这个UpdateItemRequest
?或者任何推荐的方式来构建这种复杂的表达式,例如作为某种东西的序列化形式?或者甚至更好,一些使用事务的面向对象的方式,传递更新对象的映射而不是大字符串?
谢谢。