【问题标题】:PropertyDefinition inconsistent属性定义不一致
【发布时间】:2017-06-14 10:05:28
【问题描述】:

我有以下模板,我在 cloudformation UI 中使用它来创建 dynamoDB 表。我想创建一个 PrimaryKey 作为 IDsortKey 作为 Value

的表
{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "DB Description",

  "Resources" : {
    "TableName" : {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "AttributeDefinitions": [ { 
          "AttributeName" : "ID",
          "AttributeType" : "S"
        }, { 
          "AttributeName" : "Value",
          "AttributeType" : "S"
        } ],
        "KeySchema": [
          { 
            "AttributeName": "ID", 
            "KeyType": "HASH"
          }
        ]                
      },
      "TableName": "TableName"
    }
  }
}

在 CF UI 上,我单击新堆栈,从本地计算机指向 template 文件,为堆栈命名并单击下一步。一段时间后,我收到错误消息,提示 Property AttributeDefinitions 与表的 KeySchema 和二级索引不一致

【问题讨论】:

标签: amazon-web-services amazon-dynamodb amazon-cloudformation


【解决方案1】:

由于错误原因,接受的答案是正确的,但您说您希望排序键为Value。因此,您应该更改您的 CloudFormation 以包括:

{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "DB Description",

  "Resources" : {
    "TableName" : {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "AttributeDefinitions": [ { 
          "AttributeName" : "ID",
          "AttributeType" : "S"
        }, { 
          "AttributeName" : "Value",
          "AttributeType" : "S"
        } ],
        "KeySchema": [
          { 
            "AttributeName": "ID", 
            "KeyType": "HASH"
          },
          { 
            "AttributeName": "Value", 
            "KeyType": "RANGE"
          }
        ]                
      },
      "TableName": "TableName"
    }
  }
}

【讨论】:

    【解决方案2】:

    在 AttributeDefinitions 中,您只需要定义分区和范围键,而不是其他属性。

    AttributeDefinitions 和 KeySchema 中的属性数量应该匹配并且应该完全相同。

    【讨论】:

      【解决方案3】:

      问题在于Resources.Properties.AttributeDefinitions 键必须定义用于索引或键的列。换句话说,Resources.Properties.AttributeDefinitions 中的键必须与Resources.Properties.KeySchema 中定义的相同键匹配。

      AWS 文档:

      AttributeDefinitions:描述表和索引的关键架构的 AttributeName 和 AttributeType 对象列表。

      所以生成的模板如下所示:

      {
        "AWSTemplateFormatVersion" : "2010-09-09",
      
        "Description" : "DB Description",
      
        "Resources" : {
          "TableName" : {
          "Type" : "AWS::DynamoDB::Table",
          "Properties" : {
            "AttributeDefinitions": [ { 
              "AttributeName" : "ID",
              "AttributeType" : "S"
            } ],
            "ProvisionedThroughput":{
              "ReadCapacityUnits" : 1,
              "WriteCapacityUnits" : 1
            },
            "KeySchema": [
              { 
                "AttributeName": "ID", 
                "KeyType": "HASH"
              }
             ] ,               
            "TableName": "table5"
          }
         }
        }
      }
      

      【讨论】:

      • 如果我们从 AttributeDefinitions 中删除 'value' 属性,我们如何将 'Value' 列添加到表中?
      • stackoverflow.com/questions/25606135/…经过研究,创建表时无需定义所有列,只需定义索引,然后您可以在插入新行时“随时随地”添加属性跨度>
      • stackoverflow.com/questions/47385177/… 另一个解释这一点的答案
      • 问题中说Value应该是排序键,所以应该包含在KeySchema中,而不是从AttributeDefinitions中删除。
      猜你喜欢
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多