关键架构中的属性数量必须与属性定义中定义的属性数量相匹配

我想创build一个简单的表,使用DynamoDB的JavaScript shell,我得到这个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 } 

下面是我试图创build的表格:

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

我不想将范围添加到关键架构。 任何想法如何解决它?

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

也就是说,创build表时,您需要指定关键模式(属性名称和types)。 那么,你不需要指定任何非关键属性。 您可以稍后放置具有任何属性的项目(当然,必须包含键)。

在文档页面中 , AttributeDefinitions被定义为:

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

在创build表时, AttributeDefinitions字段仅用于哈希和/或范围键。 在你的第一种情况下,只有散列键(数字1),而你提供了2个AttributeDefinitions。 这是exception的根本原因。

TL; DR不要在AttributeDefinitions包含任何非关键属性定义。

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

我也有这个问题,我会在这里发布什么错了,以防万一它帮助别人。

在我的CreateTableRequest中,我有一个GlobalSecondaryIndexes的空数组。

 CreateTableRequest createTableRequest = new CreateTableRequest { TableName = TableName, ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 2, WriteCapacityUnits = 2 }, KeySchema = new List<KeySchemaElement> { new KeySchemaElement { AttributeName = "Field1", KeyType = KeyType.HASH }, new KeySchemaElement { AttributeName = "Field2", KeyType = KeyType.RANGE } }, AttributeDefinitions = new List<AttributeDefinition>() { new AttributeDefinition { AttributeName = "Field1", AttributeType = ScalarAttributeType.S }, new AttributeDefinition { AttributeName = "Field2", AttributeType = ScalarAttributeType.S } }, //GlobalSecondaryIndexes = new List<GlobalSecondaryIndex> //{ //} }; 

在表格创build中注释这些行解决了我的问题。 所以我想列表必须为空,而不是空的。