DynamoDB put-item ConditionalCheckFailedException

Mar*_*ler 4 amazon-dynamodb

鉴于下面定义的表模式(create-table.json)我在调用后put-item使用add-event1.json后跟以下错误add-event2.json:

调用PutItem操作时发生客户端错误(ConditionalCheckFailedException):条件请求失败

为什么ConditionExpression不允许我写两个记录?(我希望在第二次手术后有2条记录)

我怀疑这是因为使用了非关键条件,但我没有在文档中看到任何表明缺乏对非关键条件的支持的内容.

创建表

$ aws dynamodb create-table --cli-input-json file://create-table.json
Run Code Online (Sandbox Code Playgroud)

创建-table.json

{
    "TableName": "EVENTS_TEST",
    "KeySchema": [
      { "AttributeName": "aggregateId", "KeyType": "HASH" },
      { "AttributeName": "streamRevision", "KeyType": "RANGE" }
    ],
    "AttributeDefinitions": [
      { "AttributeName": "aggregateId", "AttributeType": "S" },
      { "AttributeName": "streamRevision", "AttributeType": "N" }
    ],
    "ProvisionedThroughput": {
      "ReadCapacityUnits": 10,
      "WriteCapacityUnits": 10
    }
 }
Run Code Online (Sandbox Code Playgroud)

添加第一条记录

$ aws dynamodb put-item --cli-input-json file://add-event1.json
Run Code Online (Sandbox Code Playgroud)

附加event1.json

{
  "TableName": "EVENTS_TEST",
  "Item": {
    "aggregateId": { "S": "id" },
    "id": { "S": "119" },
    "context": { "S": "*" },
    "aggregate": { "S": "*" },
    "streamRevision": { "N": "0" },
    "commitId": { "S": "1119" },
    "commitSequence": { "N": "0" },
    "commitStamp": { "N": "1470185631511" },
    "dispatched": { "BOOL": false },
    "payload": { "S": "{ \"event\": \"bla\" }" }
  },
  "ExpressionAttributeNames": { "#name": "aggregate" },
  "ConditionExpression": "attribute_not_exists(aggregateId) and attribute_not_exists(streamRevision) and #name <> :name and context <> :ctx",
  "ExpressionAttributeValues": {
    ":name": { "S": "*" },
    ":ctx": { "S": "*" }
  }
}
Run Code Online (Sandbox Code Playgroud)

添加第二条记录

$ aws dynamodb put-item --cli-input-json file://add-event2.json
Run Code Online (Sandbox Code Playgroud)

附加event2.json

{
  "TableName": "EVENTS_TEST",
  "Item": {
    "aggregateId": { "S": "id" },
    "id": { "S": "123" },
    "context": { "S": "myCtx" },
    "aggregate": { "S": "myAgg" },
    "streamRevision": { "N": "0" },
    "commitId": { "S": "1123" },
    "commitSequence": { "N": "0" },
    "commitStamp": { "N": "1470185631551" },
    "dispatched": { "BOOL": false },
    "payload": { "S": "{ \"event\": \"bla2\" }" }
  },
  "ExpressionAttributeNames": { "#name": "aggregate" },
  "ConditionExpression": "aggregateId <> :id and streamRevision <> :rev and #name <> :name and context <> :ctx",
  "ExpressionAttributeValues": {
     ":id": { "S": "id" },
     ":rev": { "N": "0" },
     ":name": { "S": "myAgg" },
     ":ctx": { "S": "myCtx" }
  }
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*ema 6

您的目标是保存两个记录.这里有2个问题

  1. 通过选择散列和范围键,无法保存两个记录

散列和范围键的组合使记录唯一.事件1和事件2具有相同的散列和范围键值.因此第二个put-item将简单地覆盖第一个记录.

  1. 您的ConditionExpression会阻止记录2替换记录1

在放入记录之前评估ConditionExpression.您的表达式失败,因为当您要插入事件2时,DynamoDB发现已存在具有aggregateId"id1"的记录.条件在"attribute_not_exists(aggregateId)"上失败,并且您收到ConditionalCheckFailedException此表达式阻止记录2覆盖记录1.

如果要保存两个记录,则必须提供另一种散列键和/或范围键,以更好地表示数据项的唯一性.你无法用ConditionExpression解决这个问题.

  • 我认为这与“attribute_not_exists(aggregateId)”无关,它仅在 record1 的 ConditionExpression 中。已存储并完成。我认为 record2 的 PutItem 失败,因为 record2 的 ConditionExpression 失败:“aggregateId &lt;&gt; :id” (2认同)