2013-07-20 4 views
13

Ich bin neu in Javascript und node.js und fragte mich, ob jemand mir helfen kann, herauszufinden, die Syntax eines neuen Elements auf eine vorhandene Tabelle auf AWS Dynamodb durch ihr node.js SDK. Hier ist, was ich bisher habe. Gibt es ein Beispiel für das, was ich versuche? Wenn mir jemand in die richtige Richtung zeigen könnte, würde es sehr geschätzt werden.Setzen Sie Element auf DynamoDB Tabelle mit AWS SDK für Node.js

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

Antwort

3

ich Ihre "id" erwarten numerisch zu sein ...

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

Beachten Sie, dass mit DynamoDB Sie den Datentyp angeben (N »numerisch, S» string, B »binary) bei der Tabellenerstellung, nur für den Primärschlüssel (HashKey oder HashKey + RangeKey). Alle anderen Spalten dürfen in ihrem Datentyp variieren und können als Schlüssel-Wert-Paare angesehen werden. Daher muss DynamoDB den Datentyp immer mit den Elementattributen codieren.

23
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 .................."); 
+0

[DynamoDB-Marshaler] (https://github.com/CascadeEnergy/dynamoDb-marshaler) den Schmerz der Formatierung helfen erleichtern – tsuz

Verwandte Themen