2016-12-13 130 views
1

我想创建一个表,并希望使用Dynamodb(NodeJs)创建6-7列/属性。我创建了一个表格,但我无法添加超过2个属性。我是这个平台的新手,任何人都可以帮助我在一个表格中创建多个属性。使用nodejs创建表Dynamodb?

回答

1

在DynamoDB上,您必须仅为您的表定义Hash Key和可选的Sort Key。其余的属性不必定义!你可以推送任何你想要的数据。

查看下面的示例,根据the official docs

我正在用Hash创建表MoviesYear和Sort:Title。 然后我创建一个电影更多的属性:

var AWS = require("aws-sdk"); 

AWS.config.update({ 
    region: "us-west-2", 
    endpoint: "http://localhost:8000" 
}); 

var client = new AWS.DynamoDB(); 
var documentClient = new AWS.DynamoDB.DocumentClient(); 

var tableName = "Movies"; 

var params = { 
    TableName: tableName, 
    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 
    } 
}; 

client.createTable(params, function(tableErr, tableData) { 
    if (tableErr) { 
     console.error("Error JSON:", JSON.stringify(tableErr, null, 2)); 
    } else { 
     console.log("Created table successfully!"); 
    } 

    // Adding Batman movie to our collection 
    var params = { 
     TableName: tableName, 
     Item: { 
      "year": 2005, 
      "title": "Batman Begins", 
      "info": { 
       "plot": "A young Bruce Wayne (Christian Bale) travels to the Far East.", 
       "rating": 0 
      } 
     } 
    }; 

    console.log("Adding a new item..."); 
    documentClient.put(params, function(err, data) { 
     if (err) { 
      console.error("Error JSON:", JSON.stringify(err, null, 2)); 
     } else { 
      console.log("Added item successfully!"); 
     } 
    }); 
}); 
1

在dynamoDB,当你添加一个项目到数据库属性将自动创建。

创建表时,我们只指定主键和一个可选的排序键。

相关问题