node.js:如何在创build表时在DynamoDB中添加非键属性?

我正在使用dynamoDB本地。 我想创build一个具有6个属性的表,其中只有一个是key 。 我怎么做? 在keySchema指定key属性,并在AttributeDefinitions指定所有AttributeDefinitions

 var params = { TableName : "Movies", KeySchema: [ { AttributeName: "year", KeyType: "HASH"}, //Partition key ], AttributeDefinitions: [ { AttributeName: "year", AttributeType: "N" }, { AttributeName: "title", AttributeType: "S" } ], ProvisionedThroughput: { ReadCapacityUnits: 10, WriteCapacityUnits: 10 } }; dynamodb.createTable(params, function(err, data) { if (err) { console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2)); } else { console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2)); } }); 

你收到以下错误?

一个或多个参数值无效:KeySchema中的属性数量与AttributeDefinitions中定义的属性数量不完全匹配

这是因为你的AttributeDefinitions包含一个在KeySchema没有定义的KeySchema 。 如果只打算使用HASH键,而不需要RANGE键,则可以从AttributeDefinitions删除title AttributeDefinitions

DynamoDB是无模式的,因此您不需要在AttributeDefinitions包含任何非关键属性定义。 将项目放入表格中时,可以添加任何其他属性( 必须包含分区/sorting键 )。

下面的代码将创build一个只有一个HASH (Partition) key

 var dynamodb = new AWS_SDK.DynamoDB(); var params = { TableName : "MyNewTable", KeySchema: [ { AttributeName: "year", KeyType: "HASH"}, //Partition key //{ AttributeName: "title", KeyType: "RANGE"}, //Sort key ], AttributeDefinitions: [ { AttributeName: "year", AttributeType: "N" }, // { AttributeName: "title", AttributeType: "S" } ], ProvisionedThroughput: { ReadCapacityUnits: 10, WriteCapacityUnits: 10 } }; dynamodb.createTable(params, function(err, data) { if (err) { console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2)); } else { console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2)); } 

有关更多信息,可以参考DynamoDB服务上的createTable函数的AWS SDK文档 。

希望这可以帮助!