dynamodb 中原子事务的实现 [英] Implementation of Atomic Transactions in dynamodb

查看:24
本文介绍了dynamodb 中原子事务的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 dynamodb 中有一个表,我需要一次更新多个相关项目(由于 400kb 大小限制,我不能将所有数据放在一个项目中).如何确保成功更新多行或不更新.

I have a table in dynamodb, where I need to update multiple related items at once(I can't put all data in one item because of 400kb size limit). How can I make sure that either multiple rows are updated successfully or none.

最终目标是在更新后读取一致的数据.

End goal is to read consistent data after update.

推荐答案

2018 年 11 月 27 日,宣布了 Dynamo DB 的交易. 来自链接文章:

DynamoDB 事务在单个 AWS 账户和区域内的一个或多个表中为开发人员提供原子性、一致性、隔离性和持久性 (ACID).在构建需要对多个项目进行协调插入、删除或更新作为单个逻辑业务操作的一部分的应用程序时,您可以使用事务.DynamoDB 是唯一支持跨多个分区和表的事务的非关系型数据库.

DynamoDB transactions provide developers atomicity, consistency, isolation, and durability (ACID) across one or more tables within a single AWS account and region. You can use transactions when building applications that require coordinated inserts, deletes, or updates to multiple items as part of a single logical business operation. DynamoDB is the only non-relational database that supports transactions across multiple partitions and tables.

新的 API 是:

  • TransactWriteItems,一种包含写入集的批处理操作,具有一个或多个 PutItem、UpdateItem 和 DeleteItem 操作.TransactWriteItems 可以选择检查在进行更新之前必须满足的先决条件.这些条件可能涉及与写入集中相同或不同的项目.如果不满足任何条件,则交易被拒绝.

  • 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,包含读取集的批处理操作,具有一个或多个 GetItem 操作.如果对属于活动写入事务的项目发出 TransactGetItems 请求,则会取消读取事务.要获取之前提交的值,您可以使用标准读取.

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.

链接的文章还有一个 JavaScript 示例:

The linked article also has a JavaScript example:

data = await dynamoDb.transactWriteItems({
  TransactItems: [
    {
      Update: {
        TableName: 'items',
        Key: { id: { S: itemId } },
        ConditionExpression: 'available = :true',
        UpdateExpression: 'set available = :false, ' +
            'ownedBy = :player',
        ExpressionAttributeValues: {
          ':true': { BOOL: true },
          ':false': { BOOL: false },
          ':player': { S: playerId }
        }
      }
    },
    {
      Update: {
        TableName: 'players',
        Key: { id: { S: playerId } },
        ConditionExpression: 'coins >= :price',
        UpdateExpression: 'set coins = coins - :price, ' +
            'inventory = list_append(inventory, :items)',
        ExpressionAttributeValues: {
          ':items': { L: [{ S: itemId }] },
          ':price': { N: itemPrice.toString() }
        }
      }
    }
  ]
}).promise();

这篇关于dynamodb 中原子事务的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆