TransactGetItems
通过使用 TransactGetItems 请求映射文档,您可以指示 Amazon AppSync DynamoDB 解析器对 DynamoDB 发出 TransactGetItems 请求以检索多个项目(可能位于多个表中)。对于此请求模板,您必须指定以下各项:
-
从中检索项目的每个请求项目的表名称
-
要从每个表中检索的每个请求项的键
DynamoDB TransactGetItems 限制适用,并且无法提供任何条件表达式。
TransactGetItems 映射文档具有以下结构:
{ "version": "2018-05-29", "operation": "TransactGetItems", "transactItems": [ ## First request item { "table": "table1", "key": { "foo": ... typed value, "bar": ... typed value }, "projection" : { ... } }, ## Second request item { "table": "table2", "key": { "foo": ... typed value, "bar": ... typed value }, "projection" : { ... } } ] }
字段定义如下:
TransactGetItems 字段
-
version -
模板定义版本。仅支持
2018-05-29。该值为必填项。 -
operation -
要执行的 DynamoDB 操作。要执行
TransactGetItemsDynamoDB 操作,该字段必须设置为TransactGetItems。该值为必填项。 -
transactItems -
要包含的请求项目。该值是请求项目的数组。必须提供至少一个请求项目。该
transactItems值为必填项。-
table -
要从中检索项目的 DynamoDB 表。该值是表名的字符串。该
table值为必填项。 -
key -
DynamoDB 键,表示要检索的项目的主键。DynamoDB 项目可能具有单个哈希键,也可能具有哈希键和排序键,具体取决于表结构。有关如何指定“类型化值”的更多信息,请参阅类型系统(请求映射)。
projection-
用于指定从 DynamoDB 操作返回的属性的投影。有关投影的更多信息,请参阅投影。该字段是可选的。
-
要记住的事项:
-
如果事务成功,
items块中检索项目的顺序将与请求项目的顺序相同。 -
事务以全部或全不的方式执行。如果任何请求项目导致错误,则整个交易都不会执行,并返回错误详细信息。
-
无法检索的请求项目不是错误。相反,null 元素会出现在相应位置的 items 块中。
-
如果一个事务的错误是 TransactionCanceledException,则
cancellationReasons块将被填充。cancellationReasons块中取消原因的顺序将与请求项目的顺序相同。 -
TransactGetItems限制为 100 个请求项目。
对于以下示例请求映射模板:
{ "version": "2018-05-29", "operation": "TransactGetItems", "transactItems": [ ## First request item { "table": "posts", "key": { "post_id": { "S": "p1" } } }, ## Second request item { "table": "authors", "key": { "author_id": { "S": a1 } } } ] }
如果事务成功并且只检索第一个请求的项目,则 $ctx.result 中可用的调用结果如下所示:
{ "items": [ { // Attributes of the first requested item "post_id": "p1", "post_title": "title", "post_description": "description" }, // Could not retrieve the second requested item null, ], "cancellationReasons": null }
如果事务由于第一个请求项目引起的 TransactionCanceledException 而失败,则 $ctx.result 中可用的调用结果如下所示:
{ "items": null, "cancellationReasons": [ { "type":"Sample error type", "message":"Sample error message" }, { "type":"None", "message":"None" } ] }
$ctx.error 包含有关该错误的详细信息。键 items 和 cancellationReasons 保证出现在 $ctx.result 中。
有关更完整的示例,请按照此处适用于 AppSync 的 DynamoDB 事务教程“教程:DynamoDB 事务解析器”进行操作。