2016-12-13 54 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,當你添加一個項目到數據庫屬性將自動創建。

創建表時,我們只指定主鍵和一個可選的排序鍵。

相關問題