关键架构中的属性数量必须与属性定义中定义的属性数量匹配 [英] Number of attributes in key schema must match the number of attributes defined in attribute definitions

查看:280
本文介绍了关键架构中的属性数量必须与属性定义中定义的属性数量匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用DynamoDB javascript shell创建一个简单表,并且遇到了以下异常:

I’m trying to create a simple table using DynamoDB javascript shell and I’m getting this exception:


    {   
    "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中,则可以正常工作。在工作表下面:

However if I add the second attribute to the keySchema, it works fine. Below a the working table:


    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); 
    });

我不想将范围添加到关键架构中。知道如何解决吗?

I don’t want to add the range to key schema. Any idea how to fix it?

推荐答案

DynamoDB是无模式的(关键模式除外)

也就是说,在创建表时,您确实需要指定键架构(属性名称和类型)。嗯,您不需要指定任何非关键属性。您以后可以放置具有任何属性的项目(当然必须包括键)。

That is to say, you do need to specify the key schema (attribute name and type) when you create the table. Well, you don't need to specify any non-key attributes. You can put an item with any attribute later (must include the keys of course).

来自文档页面 AttributeDefinitions 定义为:


描述表和索引的关键模式的属性数组。

An array of attributes that describe the key schema for the table and indexes.

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

When you create table, the AttributeDefinitions field is used for the hash and/or range keys only. In your first case, there is hash key only (number 1) while you provide 2 AttributeDefinitions. This is the root cause of the exception.

TL; DR AttributeDefinitions 。

这篇关于关键架构中的属性数量必须与属性定义中定义的属性数量匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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