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文档 。
希望这可以帮助!
- AWS Lambda和Dynamo:一个或多个参数值无效
- 从S3文件读取JSON并使用带有NodeJS运行时的Lambda将logging插入到dynamoDB中
- AWS – 在DynamoDB表中放置一个GeoPoing并对其进行查询 – node.js
- AWS DynamoDb查询表
- 为什么使用Limit和FilterExpression进行DynamoDB扫描不会返回符合filter要求的项目?
- Amazon Web服务DynamoDB请求缓慢
- 如何使用非主键字段查询DynamoDB?
- KeyConditionExpression错误和空错误响应
- 如何使用Lambda和DynamoDB在date范围之间扫描?