简体   繁体   English

BatchPutItem 与 DynamoDB 中的 PutItem

[英]BatchPutItem vs PutItem in DynamoDB

I have a use case where we don't get that many cases where there are list of entries in a single request to be updated and mostly, it is the case where only a single entry is requested to be updated.我有一个用例,在这个用例中,我们没有得到太多在单个请求中有条目列表要更新的情况,而且大多数情况下,只有一个条目被请求更新。 But in future, this might grow and hence I was thinking of using BatchPutItem instead of PutItem.但在未来,这可能会增长,因此我正在考虑使用 BatchPutItem 而不是 PutItem。

  • Are there any disadvantages using BatchPutItem when the there is only a single item to be updated in the request?当请求中只有一个项目要更新时,使用 BatchPutItem 有什么缺点吗?
  • I think the RCUs consumed are the same, but are there any other differences or the BatchPutItem will behave same as PutItem when there is only a single entry in the request?我认为消耗的 RCU 是相同的,但是否存在任何其他差异,或者当请求中只有一个条目时 BatchPutItem 的行为与 PutItem 相同?

From a m.netary cost perspective, it shouldn't matter - the WCUs consumed are the same.从 m.netary 成本的角度来看,这应该无关紧要 - 消耗的 WCU 是相同的。 The benefit of the Batch API is that fewer requests go over the wire and thus reduce the overall latency for writing multiple items.批处理 API 的好处是通过线路减少请求 go,从而减少写入多个项目的总体延迟。

The error handling is different between the two APIs and it requires a bit of additional complexity to deal with BatchPutItem .两个 API 的错误处理不同,处理BatchPutItem需要一些额外的复杂性。 Even if the BatchPutItem request is successful, individual item writes may have failed and you need to inspect the response from the API and retry and failed writes yourself.即使BatchPutItem请求成功,个别项目写入也可能失败,您需要检查来自 API 的响应并重试并自行写入失败。

The regular PutItem would fail in a more straightforward way by returning an error / raising an exception.常规PutItem将通过返回错误/引发异常以更直接的方式失败。

This is just something to keep in mind and if you're going to use BatchWriteItem anyway, you'll have to build logic for that in any case and this wouldn't be a drawback.这只是要记住的事情,如果您无论如何都要使用BatchWriteItem ,则无论如何都必须为此构建逻辑,这不是缺点。

tl;dr: Using BatchWriteItem with a single item should have no drawbacks in your case, because you'll have to build the retry logic anyway. tl;dr:在您的情况下,将BatchWriteItem与单个项目一起使用应该没有任何缺点,因为无论如何您都必须构建重试逻辑。

Maurice's answer already covers most of the non-differences between a single-operation BatchWriteItem (sic) and a standalone PutItem . Maurice 的回答已经涵盖了单操作BatchWriteItem (原文如此)和独立PutItem之间的大部分非差异。 But you should be aware that there's also a difference between the capabilities of these operations:但是您应该意识到这些操作的功能之间也存在差异:

The operation that a BatchWriteItem can do on items are very limited - you can delete an item, or completely replace it, but that's it. BatchWriteItem可以对项目执行的操作非常有限——您可以删除一个项目,或者完全替换它,仅此而已。

In contrast, the stand-alone operations can do more:相比之下,单机操作可以做的更多:

  1. a stand-alone PutItem operation can be conditional (only replace the item if some condition matches on the previous value of the item).独立的PutItem操作可以是有条件的(仅当某些条件与项目的先前值匹配时才替换项目)。 The operation can also return the previous value of the item, at no extra cost.该操作还可以返回项目的先前值,无需额外费用。

  2. a stand-alone UpdateItem operation can do even more: In addition to being conditional and returning the previous value of an item, this operation can also modify attributes of the item - not replace it entirely.独立的UpdateItem操作可以做更多的事情:除了有条件地返回项目的先前值之外,此操作还可以修改项目的属性 - 而不是完全替换它。 These modification can even include expressions - eg, increment an attribute in-place.这些修改甚至可以包括表达式——例如,就地增加一个属性。

These additional capabilities are not available through BatchWriteItem .这些附加功能无法通过BatchWriteItem If you need any of these capabilities, you will have to use PutItem or UpdateItem directly, and won't be able to use a single-request BatchWriteItem .如果您需要这些功能中的任何一项,则必须直接使用PutItemUpdateItem ,并且无法使用单请求BatchWriteItem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM