简体   繁体   English

使用nodejs Dynamodb 创建表?

[英]Create table using nodejs Dynamodb?

I want to create a table and want to create 6-7 columns/attributes using Dynamodb(NodeJs).我想创建一个表并想使用 Dynamodb(NodeJs) 创建 6-7 个列/属性。 I have created a table but I am not able to add more than 2 attributes.I am new to this platform, can anyone help me to create multiple attributes in one table.我创建了一个表,但我不能添加 2 个以上的属性。我是这个平台的新手,谁能帮我在一个表中创建多个属性。

On DynamoDB, you must define only the Hash Key and optionally a Sort Key for your table.在 DynamoDB 上,您必须仅为您的表定义Hash Key和可选的Sort Key The rest of the attributes don't have to be defined!其余的属性不必定义! You can push whatever data you want.你可以推送任何你想要的数据。

Check out the example below, based on the official docs .查看下面基于官方文档的示例。

I'm creating a table Movies with Hash: Year and Sort: Title .我正在创建一个表Movies与 Hash: Year和 Sort: Title Then I'm creating a movie with more attributes:然后我正在创建具有更多属性的电影:

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

In dynamoDB, attributes will created automatically when you add an Item to the db.在 dynamoDB 中,当您向数据库添加项目时,将自动创建属性。

While creating a table, we only specify the primary key and an optional sort key.创建表时,我们只指定主键和可选的排序键。

here is an example of creating a table on DynamoDB using nodejs AWS-SDK这是使用 nodejs AWS-SDK 在 DynamoDB 上创建表的示例

 // Imports const AWS = require('aws-sdk') AWS.config.update({ region: 'eu-west-3' }); // Declare local variables const dynamo = new AWS.DynamoDB(); createTable('your table name') .then(data => console.log(data)) function createTable (tableName) { const params = { TableName: tableName, AttributeDefinitions: [ // required attributes to be used as keys { AttributeName: 'id', AttributeType: 'N' } ], KeySchema: [ { AttributeName: 'id', KeyType: 'HASH' } ], ProvisionedThroughput: { ReadCapacityUnits: 5, // Eventually Consistent Model WriteCapacityUnits: 5 } } return new Promise((resolve, reject) => { dynamo.createTable(params, (err, data) => { if(err) reject(err); else resolve(data); }) }) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM