本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
BatchPutItem
BatchPutItem
请求映射文档允许您告诉 D Amazon AppSync ynamoDB 解析器向 DynamoDB 发出BatchWriteItem
请求,要求将多个项目放置在多个表中。对于此请求模板,您必须指定以下各项:
-
要将项目放置在其中的表名称
-
要放置在每个表中的完整项目
DynamoDB BatchWriteItem
限制适用,并且无法提供任何条件表达式。
BatchPutItem
映射文档具有以下结构:
{ "version" : "2018-05-29", "operation" : "BatchPutItem", "tables" : { "table1": [ ## Item to put { "foo" : ... typed value, "bar" : ... typed value }, ## Item2 to put { "foo" : ... typed value, "bar" : ... typed value }], "table2": [ ## Item3 to put { "foo" : ... typed value, "bar" : ... typed value }, ## Item4 to put { "foo" : ... typed value, "bar" : ... typed value }], } }
字段定义如下:
BatchPutItem 字段
-
version
-
模板定义版本。仅支持
2018-05-29
。该值为必填项。 -
operation
-
要执行的 DynamoDB 操作。要执行
BatchPutItem
DynamoDB 操作,该字段必须设置为BatchPutItem
。该值为必填项。 -
tables
-
要在其中放置项目的 DynamoDB 表。每个表条目表示要为该特定表插入的 DynamoDB 项目列表。必须提供至少一个表。该值为必填项。
要记住的事项:
-
如果成功,响应中将返回完全插入的项目。
-
如果尚未向表中插入项目,null 元素将显示在该表的数据块中。
-
根据在请求映射模板中提供插入的项目的顺序,按表对这些项目进行排序。
-
BatchPutItem
中的每个Put
命令都是原子性的,但可以部分处理一个批次。如果由于错误而部分处理一个批处理,则未处理的键将作为 unprocessedKeys 块内的调用结果的一部分返回。 -
BatchPutItem
限制为 25 个项目。 -
不支持与冲突检测功能一起使用此操作。两者同时使用可能会导致错误。
对于以下示例请求映射模板:
{ "version": "2018-05-29", "operation": "BatchPutItem", "tables": { "authors": [ { "author_id": { "S": "a1" }, "author_name": { "S": "a1_name" } }, ], "posts": [ { "author_id": { "S": "a1" }, "post_id": { "S": "p2" }, "post_title": { "S": "title" } } ], } }
$ctx.result
中可用的调用结果如下所示:
{ "data": { "authors": [ null ], "posts": [ # Was inserted { "author_id": "a1", "post_id": "p2", "post_title": "title" } ] }, "unprocessedItems": { "authors": [ # This item was not processed due to an error { "author_id": "a1", "author_name": "a1_name" } ], "posts": [] } }
$ctx.error
包含有关该错误的详细信息。保证键数据、unprocessedItems 和请求映射模板中提供的每个表键都出现在调用结果中。已插入的项目存在于数据块中。尚未处理的项目将在数据块中标记为 null 并置于 unprocessedItems 块中。
要获得更完整的示例,请按照 DynamoDB 批处理教程进行操作,这里有 AppSync 教程:Dynamo DB 批处理解析器。