密钥模式中的属性数必须与属性定义中定义的属性数相匹配

NAb*_*bas 82 amazon-dynamodb dynamo-local

我正在尝试使用DynamoDB javascript shell创建一个简单的表,我得到了这个异常:


    {   
    "message": "The number of attributes in key schema must match the number of attributes defined in attribute definitions.",
    "code": "ValidationException",
    "time": "2015-06-16T10:24:23.319Z",
    "statusCode": 400,
    "retryable": false 
    }

下面是我要创建的表:


    var params = {
        TableName: 'table_name',
        KeySchema: [ 
            { 
                AttributeName: 'hash_key_attribute_name',
                KeyType: 'HASH',
            },

        ],
        AttributeDefinitions: [ 
            {
                AttributeName: 'hash_key_attribute_name',
                AttributeType: 'S', 
            },
            {
                AttributeName: 'attribute_name_1',
                AttributeType: 'S', 
            }
        ],
        ProvisionedThroughput: { 
            ReadCapacityUnits: 1, 
            WriteCapacityUnits: 1, 
        },


    };
    dynamodb.createTable(params, function(err, data) {
        if (err) print(err); 
        else print(data); 
    });

但是,如果我将第二个属性添加到keySchema,它可以正常工作.在工作台下面:


    var params = {
        TableName: 'table_name',
        KeySchema: [ 
            { 
                AttributeName: 'hash_key_attribute_name',
                KeyType: 'HASH',
            },
            { 
                AttributeName: 'attribute_name_1', 
                KeyType: 'RANGE', 
            }

        ],
        AttributeDefinitions: [ 
            {
                AttributeName: 'hash_key_attribute_name',
                AttributeType: 'S', 
            },
            {
                AttributeName: 'attribute_name_1',
                AttributeType: 'S', 
            }
        ],
        ProvisionedThroughput: { 
            ReadCapacityUnits: 1, 
            WriteCapacityUnits: 1, 
        },


    };
    dynamodb.createTable(params, function(err, data) {
        if (err) print(err); 
        else print(data); 
    });

我不想将范围添加到密钥架构.知道怎么解决吗?

Min*_*Liu 180

DynamoDB是无模式的(除了密钥模式)

也就是说,您需要在创建表时指定密钥架构(属性名称和类型).好吧,您不需要指定任何非键属性.您可以稍后放置具有任何属性的项目(当然必须包含键).

文档页面,AttributeDefinitions定义为:

描述表和索引的键架构的属性数组.

创建表时,该AttributeDefinitions字段仅用于散列和/或范围键.在第一种情况下,只提供哈希键(数字1),同时提供2个AttributeDefinitions.这是异常的根本原因.

TL; DR 不包括任何非关键属性定义AttributeDefinitions.

  • 我相信有一个例外,如果该键在索引中用作`hash`或`range`键,那么非键属性应该在`AttributeDefinitions`中 (9认同)

Gab*_* Wu 16

在"AttributeDefinitions"中使用非键属性时,必须将其用作索引,否则它将违背dynamodb的工作方式.看到 链接

因此,如果您不将其用作索引或主键,则无需在"AttributeDefinitions"中放置非键属性.

var params = {
        TableName: 'table_name',
        KeySchema: [ // The type of of schema.  Must start with a HASH type, with an optional second RANGE.
            { // Required HASH type attribute
                AttributeName: 'UserId',
                KeyType: 'HASH',
            },
            { // Optional RANGE key type for HASH + RANGE tables
                AttributeName: 'RemindTime', 
                KeyType: 'RANGE', 
            }
        ],
        AttributeDefinitions: [ // The names and types of all primary and index key attributes only
            {
                AttributeName: 'UserId',
                AttributeType: 'S', // (S | N | B) for string, number, binary
            },
            {
                AttributeName: 'RemindTime',
                AttributeType: 'S', // (S | N | B) for string, number, binary
            },
            {
                AttributeName: 'AlarmId',
                AttributeType: 'S', // (S | N | B) for string, number, binary
            },
            // ... more attributes ...
        ],
        ProvisionedThroughput: { // required provisioned throughput for the table
            ReadCapacityUnits: 1, 
            WriteCapacityUnits: 1, 
        },
        LocalSecondaryIndexes: [ // optional (list of LocalSecondaryIndex)
            { 
                IndexName: 'index_UserId_AlarmId',
                KeySchema: [ 
                    { // Required HASH type attribute - must match the table's HASH key attribute name
                        AttributeName: 'UserId',
                        KeyType: 'HASH',
                    },
                    { // alternate RANGE key attribute for the secondary index
                        AttributeName: 'AlarmId', 
                        KeyType: 'RANGE', 
                    }
                ],
                Projection: { // required
                    ProjectionType: 'ALL', // (ALL | KEYS_ONLY | INCLUDE)
                },
            },
            // ... more local secondary indexes ...
        ],
    };
    dynamodb.createTable(params, function(err, data) {
        if (err) ppJson(err); // an error occurred
        else ppJson(data); // successful response

    });
Run Code Online (Sandbox Code Playgroud)


Gru*_*Gru 9

AttrubuteDefinitions仅当您要在中使用该属性时才声明该属性KeySchema

或者

当这些属性将被用于GlobalSecondaryIndexesLocalSecondaryIndexes

对于使用 yaml 文件的任何人:

示例1:

假设您有 3 个属性 -> id、status、createdAt。这里的id是KeySchema

    AuctionsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: AuctionsTable
        BillingMode: PAY_PER_REQUEST
        
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S

        KeySchema:
          - AttributeName: id 
            KeyType: HASH
Run Code Online (Sandbox Code Playgroud)

示例2:

对于相同的属性(即 id、status 和createdAt),如果您有GlobalSecondaryIndexesLocalSecondaryIndexes,那么您的 yaml 文件如下所示:

AuctionsTable:
  Type: AWS::DynamoDB::Table
  Properties:
    TableName: AuctionsTable-${self:provider.stage}
    BillingMode: PAY_PER_REQUEST
    AttributeDefinitions:
      - AttributeName: id
        AttributeType: S
      - AttributeName: status
        AttributeType: S
      - AttributeName: endingAt
        AttributeType: S
    KeySchema:
      - AttributeName: id
        KeyType: HASH
    GlobalSecondaryIndexes:
      - IndexName: statusAndEndDate
        KeySchema:
          - AttributeName: status
            KeyType: HASH
          - AttributeName: endingAt
            KeyType: RANGE
        Projection:
          ProjectionType: ALL
Run Code Online (Sandbox Code Playgroud)

我们在 AttributeDefinitions 中包含了 status 和createdId,只是因为我们有一个GlobalSecondaryIndex使用上述属性的属性。

原因:DynamoDB只关心Primary Key、GlobalSecondaryIndex和LocalSecondaryIndex。您不需要指定不属于上述三者的任何其他类型的属性。

DynamoDB 只关心主键、GlobalSecondaryIndex 和 LocalSecondaryIndex 进行分区。它并不关心你的物品还有哪些其他属性。