首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用nodejs Dynamodb创建表?

使用nodejs Dynamodb创建表?
EN

Stack Overflow用户
提问于 2016-12-13 22:40:32
回答 3查看 5K关注 0票数 2

我想创建一个表,并使用Dynamodb(NodeJs)创建6-7个列/属性。我已经创建了一个表,但我不能添加超过2个属性。我是这个平台的新手,有人能帮助我在一个表中创建多个属性吗?

EN

回答 3

Stack Overflow用户

发布于 2017-01-07 05:06:52

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

请查看以下基于the official docs的示例。

我正在使用Hash:YearMoviesTitle创建一个表排序。然后,我将创建一个具有更多属性的电影:

代码语言:javascript
运行
复制
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!");
        }
    });
});
票数 5
EN

Stack Overflow用户

发布于 2017-10-11 19:04:08

在dynamoDB中,当您向数据库添加项目时,将自动创建属性。

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

票数 2
EN

Stack Overflow用户

发布于 2020-04-19 09:19:55

以下是使用nodejs AWS-SDK在DynamoDB上创建表的示例

代码语言:javascript
运行
复制
// 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);
    })
  })
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41123775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档