java Dynamo DB 中的自动递增计数器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38193431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto-increment counter in Dynamo DB
提问by Sumit Gulati
I am trying to implement the auto -increment counter for hash key in dynamo db and my code fails for concurrent transactions. any help to implement the feature will be appreciated. I am new to stack over flow might be not able to specify it correctly. Any implementation will be helpful.
我正在尝试为 dynamo db 中的哈希键实现自动递增计数器,但我的代码对于并发事务失败。任何实现该功能的帮助将不胜感激。我是堆栈溢出的新手,可能无法正确指定它。任何实施都会有所帮助。
回答by Shibashis
You have two options, using dynamo db atomic counters or using optimistic locking at the time of incrementing the counter.
您有两种选择,使用 dynamo db 原子计数器或在递增计数器时使用乐观锁定。
Documentation for atomic counters: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.AtomicCounters
原子计数器的文档:https: //docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.AtomicCounters
Documentation for optimistic locking: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html
乐观锁定的文档:http: //docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html
回答by Gaz_Edge
You can also use conditional writes to check the currently stored counter value against the value you are about to write e.g check if current count is N-1 the new value N.
您还可以使用条件写入来根据您将要写入的值检查当前存储的计数器值,例如检查当前计数是否为 N-1 新值 N。
I prefer this approach as it pushes the logic of incrementing the counter to your app, but the final check on correct persistence is with DynamoDB.
我更喜欢这种方法,因为它将递增计数器的逻辑推送到您的应用程序,但对正确持久性的最终检查是使用 DynamoDB。
回答by so-random-dude
Like other SOers rightly pointed out, you can either use AtomicCountersor ConditionalUpdate. NOV 2018 onwards you have Transactionsfrom DynamoDB (Disclaimer: Havent tested transactions personally, not sure it applies for the auto-increment usecase)
就像其他 SOers 正确指出的那样,您可以使用AtomicCounters或ConditionalUpdate。从2018 年 11 月起,您有来自 DynamoDB 的交易(免责声明:尚未亲自测试交易,不确定它是否适用于自动增量用例)
This is a quote from the official doc on AtomicCounter
这是AtomicCounter官方文档的引述
you can use the UpdateItem operation to implement an atomic counter—a numeric attribute that is incremented, unconditionally, without interfering with other write requests. (All write requests are applied in the order in which they were received.) With an atomic counter, the updates are not idempotent. In other words, the numeric value increments each time you call UpdateItem.
You might use an atomic counter to track the number of visitors to a website. In this case, your application would increment a numeric value, regardless of its current value. If an UpdateItem operation fails, the application could simply retry the operation. This would risk updating the counter twice, but you could probably tolerate a slight overcounting or undercounting of website visitors.
您可以使用 UpdateItem 操作来实现一个原子计数器——一个无条件递增的数字属性,不会干扰其他写入请求。(所有写请求都按照收到的顺序应用。)使用原子计数器,更新不是幂等的。换句话说,每次调用 UpdateItem 时,数值都会增加。
您可以使用原子计数器来跟踪网站访问者的数量。在这种情况下,您的应用程序将增加一个数值,而不管其当前值如何。如果 UpdateItem 操作失败,应用程序可以简单地重试该操作。这可能会冒两次更新计数器的风险,但您可能可以容忍网站访问者的轻微多计数或少计数。
Transactions
交易
Two new DynamoDB operations have been introduced for handling transactions:
TransactWriteItems, a batch operation that contains a write set, with one or more PutItem, UpdateItem, and DeleteItem operations. TransactWriteItems can optionally check for prerequisite conditions that must be satisfied before making updates. These conditions may involve the same or different items than those in the write set. If any condition is not met, the transaction is rejected.
TransactGetItems, a batch operation that contains a read set, with one or more GetItem operations. If a TransactGetItems request is issued on an item that is part of an active write transaction, the read transaction is canceled. To get the previously committed value, you can use a standard read.
引入了两个新的 DynamoDB 操作来处理事务:
TransactWriteItems,一种包含写入集的批处理操作,具有一个或多个 PutItem、UpdateItem 和 DeleteItem 操作。TransactWriteItems 可以选择检查在进行更新之前必须满足的先决条件。这些条件可能涉及与写入集中的项目相同或不同的项目。如果任何条件不满足,交易将被拒绝。
TransactGetItems,一种包含读取集的批处理操作,具有一个或多个 GetItem 操作。如果对属于活动写入事务的项目发出 TransactGetItems 请求,则取消读取事务。要获取先前提交的值,您可以使用标准读取。