amazon-dynamodb - 如何在 DynamoDB 事务中使用 UpdateItemRequest?
问题描述
我有一个 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
?或者任何推荐的方式来构建这种复杂的表达式,例如作为某种东西的序列化形式?或者甚至更好,一些使用事务的面向对象的方式,传递更新对象的映射而不是大字符串?
谢谢。
解决方案
推荐阅读
- javascript - 从字符串初始化常量
- javascript - 为 React 和 Preact 创建组件库的最佳方法是什么?
- javascript - 如何使用 javascript 将 ID 添加到 HTML href
- c++ - 关于 LoadVector 语法的说明(将 txt 文件转换为向量)
- spring - SecurityContextHolder.getContext()。获取身份验证()。GetPrincipal() 只能获取用户名
- java - 如何让我的 JavaFX 程序加载文件?
- python - 如果总和可被 10 整除,则确定有效性
- java - 石英触发预定的失火
- c# - 如何在 asp.net core 中使用删除 Json 对象
- flutter - 使用 Stream 从 FireStore 获取数据并将其映射到 Flutter 类