2017-08-08 69 views
1

我正在使用dynamoDB本地。我想创建一个具有6个属性的表格,其中只有一个是key。我怎么做?在keySchema中指定关键属性,并在AttributeDefinitions中指定所有属性?node.js:如何在创建表时在DynamoDB中添加非键属性?

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

回答

1

您是否收到以下错误?

一个或多个参数值无效:属性在 KeySchema号码,因为你AttributeDefinitions包含未中定义的属性不完全匹配 AttributeDefinitions

这是定义的属性数量KeySchema。如果您只打算使用HASH密钥,并且不需要RANGE密钥,则可以从AttributeDefinitions中删除title属性。

DynamoDB是无模式,所以你并不需要在AttributeDefinitions任何非关键属性定义。当您将一个项目放入您的表格时,您可以添加任何其他属性(必须包含分区/排序键)。

下面的代码将创建只有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)); 
    } 

欲了解更多信息,可以参考AWS SDK documentation有关DynamoDB服务createTable功能。

希望这会有所帮助!