使用适用于 Node.js 的 AWS 开发工具包将项目放在 DynamoDB 表上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17767630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:05:32  来源:igfitidea点击:

Put item on DynamoDB table using AWS SDK for Node.js

javascriptnode.jsamazon-web-servicesamazon-dynamodb

提问by yz10

I'm new to javascript and node.js and was wondering if someone can help me figure out the syntax of putting a new item onto an existing table on AWS Dynamodb through their node.js SDK. Here's what I have so far. Is there an example for what I'm trying to do? If anyone could point me in the right direction, it'd be much appreciated.

我是 javascript 和 node.js 的新手,想知道是否有人可以帮助我弄清楚通过他们的 node.js SDK 将新项目放在 AWS Dynamodb 上的现有表上的语法。这是我到目前为止所拥有的。有没有我想要做的例子?如果有人能指出我正确的方向,我将不胜感激。

var AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json');
AWS.config.update({region: 'us-east-1'});
var dynamodb = new AWS.DynamoDB();

var item = {
    // I need to put the an item with a the primary key of "id", and an attribute called "item"
    // I'm new to js and node.js, so if somebody could help me understand the documentation
    // http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/frames.html#!http%3A//docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB_20120810.html
}

dynamodb.putItem({TableName: 'log_dev', Item: item}, function(err, data){
    if (err) {
    console.log(err); // an error occurred
    } else {
    console.log(data); // successful response
    }
});

回答by Anki007

dynamoDB.putItem(
{
    "TableName": "Table1",
    "Item": {
        "Color": {"S": "white"},
        "Name": {"S": "fancy vase"},
        "Weight": {"N": "2"},
        "LastName":{"S": "Kumar"}
    }
}, function(result) {
    result.on('data', function(chunk) {
        console.log("" + chunk);
    });
});
console.log("Items are succesfully ingested in table .................."); 

回答by muhqu

I expect your "id" to be numeric...

我希望你的“id”是数字......

var item = {
    "id": {"N": 1234},
    "title": {"S": "Foobar"}
}

Note that with DynamoDB you specify the data type (N? numeric, S? string, B? binary) at table creation, only for the primary key (HashKeyor HashKey+RangeKey). All other columns are allowed to vary in their data type, and can be seen as key-value pairs. So it is essential for DynamoDB to always encode the data type with the item attributes.

请注意,使用 DynamoDB,您可以在创建表时指定数据类型(N? 数字、S? 字符串、B? 二进制),仅用于主键(HashKeyHashKey+RangeKey)。所有其他列的数据类型都允许变化,并且可以视为键值对。因此,DynamoDB 必须始终使用项目属性对数据类型进行编码。

回答by Ryan Zhou

I don't think muhqu's answer works, I believe the value of the attribute has to be a string.

我不认为 muhqu 的答案有效,我相信该属性的值必须是一个字符串。

var item = {
"id": {"N": "1234"},
"title": {"S": "Foobar"} }

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#putItem-property

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#putItem-property