Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 'BatchGetItem' for the NodeJS AWS-SDK for DynamoDB

Tags:

node.js

I am trying to get items out of a DynamoDB table using the Node JS AWS-SDK. The function getItem is working fine but BatchGetItem is harder to use.

I use the official documentation: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/Client.html#batchGetItem-property

I am looking for examples on how to use this function correctly, but I can't find any. The code I wrote is:

var params = {

"RequestItems" : {
    "Keys" : [
      {"HashKeyElement" : { "N" : "1000" } },
      {"HashKeyElement" : { "N" : "1001" } }
    ]
  }
}

db.client.batchGetItem(params, function(err, data) {
  console.log('error: '+ err);
  console.log(jsDump.parse(data));
});

I get a SerializationException: Start of list found where not expected error but as far as my NodeJS and JSON expertise goes, my syntax is correct. But it's confusing: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/API_BatchGetItems.html

In that syntax example, you have to provide the table name.

like image 897
strai Avatar asked Feb 23 '13 16:02

strai


1 Answers

I used the dynamo db client version... after an hour of research I managed to make it work...

var params = {

RequestItems: { // map of TableName to list of Key to get from each table
    Music: {
        Keys: [ // a list of primary key value maps
            {
                Artist: 'No One You Know',
                SongTitle:'Call Me Today'
                // ... more key attributes, if the primary key is hash/range
            },
            // ... more keys to get from this table ...
        ],
        AttributesToGet: [ // option (attributes to retrieve from this table)
            'AlbumTitle',
            // ... more attribute names ...
        ],
        ConsistentRead: false, // optional (true | false)
    },
    // ... more tables and keys ...
},
ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
};
docClient.batchGet(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response

});
like image 200
Giri Avatar answered Sep 21 '22 13:09

Giri