2013-07-20 13 views
13

Sono nuovo di javascript e node.js e mi chiedevo se qualcuno può aiutarmi a capire la sintassi di mettere un nuovo elemento su una tabella esistente su AWS Dynamodb attraverso il loro SDK node.js. Ecco cosa ho finora. C'è un esempio per quello che sto cercando di fare? Se qualcuno potesse indicarmi la giusta direzione, sarebbe molto apprezzato.Inserisci elemento sulla tabella DynamoDB utilizzando AWS SDK per 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 
    } 
}); 

risposta

3

mi aspetto il vostro "id" di essere numerico ...

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

Si noti che con DynamoDB si specifica il tipo di dati (N »numerico, S» stringa, B »binario) alla creazione della tabella, solo per la chiave primaria (HashKey o HashKey + RangeKey). Tutte le altre colonne possono variare nel loro tipo di dati e possono essere viste come coppie chiave-valore. Quindi è essenziale che DynamoDB codifichi sempre il tipo di dati con gli attributi dell'oggetto.

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-marshalling] (https://github.com/CascadeEnergy/dynamoDb-marshaler) aiuterà ad alleviare il dolore di formattazione – tsuz

Problemi correlati