在DynamoDB中用新项目替换旧项目 [英] Replace an old item with a new item in DynamoDB

查看:102
本文介绍了在DynamoDB中用新项目替换旧项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 https ://godoc.org/github.com/aws/aws-sdk-go-v2/service/dynamodb#DynamoDB.PutItemRequest

我的函数看起来像这样:

My function looks like:

  func (h dynamoHandler) save(selection DeliveryDate) (err error) {
      av, err := dynamodbattribute.MarshalMap(selection)
      if err != nil {
          log.WithError(err).Error("failed to marshal selection")
          return
      }

      req := h.db.PutItemRequest(&dynamodb.PutItemInput{
          TableName: aws.String(h.Table),
          Item:      av,
      })
      _, err = req.Send()

      if err != nil {
          log.WithField("table", h.Table).WithError(err).Error("putting dynamodb")
          return
      }
      return
  }

我当时的假设是由于分区键 wfr5a相同,因此较新的 chosen#2019-05-19T13:42:54 + 08:00(复合键和排序键)应该替换了该行。

I was under the assumption that since the partition key "wfr5a" is the same, the newer "chosen#2019-05-19T13:42:54+08:00" (a composite and sort key) should have replaced the row.

"OrderID (S)","StatusDate (S)","LastUpdated (S)"
"wfr5a","chosen#2019-05-19T13:42:54+08:00","2019-05-19T13:49:34+08:00"
"wfr5a","proposal#2019-05-19T13:42:54+08:00","2019-05-19T13:42:58+08:00"

不过如您所见,它创建了一个新项目。我缺少什么,所以它只替换记录,即 wfr5a只有一个唯一行

Though as you can see, it created a new item. What am I missing to so that it just replaces the record, i.e. has only one unique row for "wfr5a"

推荐答案

具有复合主键(分区键+排序键),这两个字段的组合是唯一的,而不是分区键。

when you have composite primary key(partition key + sort key) the combination of the two fields are unique and not the partition key.

这篇关于在DynamoDB中用新项目替换旧项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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