简体   繁体   English

不使用 Key 属性的 DynamoDB 更新操作

[英]DynamoDB Update operation without using the Key attribute

The use case: I have a bid table which holds the bid on Loades.用例:我有一个投标表,其中包含对 Loades 的投标。 One Load can have multiple Bids.一个负载可以有多个投标。 The Bid status is new for every bid.每次出价的出价状态都是新的。 Once the bid is accepted by the Admin(The person who put that Load up for bidding) then I need to change the status for that particular bid as "Accepted" and for other bids on the same Load the status should be "rejected".一旦管理员接受了投标(提出该负载进行投标的人),那么我需要将该特定投标的状态更改为“已接受”,而对于同一负载上的其他投标,状态应为“拒绝”。

Table Definition: Bid_id(Which is unique for every record) and Load_id(multiple entries) is my primary and sort key respectively.表定义:Bid_id(每条记录都是唯一的)和 Load_id(多个条目)分别是我的主键和排序键。

How do I handle the Update of the Non-Key attribute of the table?如何处理表的非键属性的更新?

table.update_item(
            Key={
                'load_id': load_id,
                'bid_id' : bid_id
                },
            ConditionExpression=(
                        'load_id = :id'
                        
                ),
            
            UpdateExpression='SET #attr1 = :val1',
            ExpressionAttributeValues={':val1': status, ':id': id},
            ExpressionAttributeNames={'#attr1': 'status'},
            
            ReturnValues='UPDATED_NEW'
        )

Note: The value of Status and id is read from the event body.注意:Status 和 id 的值是从事件体中读取的。

Obviously, the above code only works when I need to change the status of the bid which got accepted to "Accept".显然,上面的代码仅在我需要将被接受的投标状态更改为“接受”时才有效。 I am thinking about how can I handle the "Rejection" status of other bids on the same load.我正在考虑如何处理同一负载下其他投标的“拒绝”状态。

Could anyone help me out with this?谁能帮我解决这个问题?

I do not believe there is a batch update option in DDB.我不相信 DDB 中有批量更新选项。 You may have some luck with Transactions, which do allow updates but come with additional overhead.您可能对事务有一些运气,它确实允许更新,但会带来额外的开销。 Alternatively, you may want to consider solving this problem with a sparse index.或者,您可能需要考虑使用稀疏索引来解决此问题。

For example, lets say your main table has the following bid information例如,假设您的主表具有以下出价信息

投标表

and lets say you want to mark BID#1 as the accepted bid.假设您想将 BID#1 标记为已接受的出价。 Instead of introducing an attribute to define the accepted/rejected status, you could create a global secondary index on the accepted bid.您可以在接受的投标上创建一个全局二级索引,而不是引入一个属性来定义接受/拒绝状态。 For example例如

在此处输入图像描述

In this example, I created an additional attribute named GSIPK (you could name it anything, this is just an example).在此示例中,我创建了一个名为 GSIPK 的附加属性(您可以将其命名为任何名称,这只是一个示例)。 Logically, your index would look like this:从逻辑上讲,您的索引将如下所示:

在此处输入图像描述

Notice that the index only contains the winning bid from LOAD#1.请注意,该索引包含来自 LOAD#1 的中标。 None of the other bids are in the index because none of the other bid items have a GSIPK attribute.没有其他出价在索引中,因为没有其他出价项具有 GSIPK 属性。

This may not satisfy all your access patterns around bids.这可能无法满足您围绕出价的所有访问模式。 However, it's a decent strategy to separate winning bids from rejected bids without having to do a bulk update of attributes.但是,将中标与拒绝的出价区分开来,而无需对属性进行批量更新,这是一种不错的策略。

If you did want to create an status attribute that you could set to the rejected/accepted status, you might consider setting the default status to rejected when inserting a bid.如果您确实想创建一个可以设置为拒绝/接受状态的status属性,您可以考虑在插入投标时将默认状态设置为rejected That way, you only have to update the single accepted bid once the bidding is done.这样,您只需在出价完成后更新单个接受的出价。

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

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