12
votes

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.

7
Hello, have you got your syntax to work, can you please share your solution? thanks! Maybe you can pastebin your getItem and getBatchItem. Thanks!!!Cmag
For anyone who comes here using js in the browser, note you need batchGet not batchGetItemGraham Hesketh

7 Answers

14
votes

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

});
8
votes

I feel your pain... AWS documentation is confusing at best. I think it's caused by aging infrastructure and bad technical writing. The nodejs and JSON syntax used by the SDK reminds me of XML structure.

Anyway, I managed to get the BatchGetItem to work after a whole hour. The params should look like below:

{
    "RequestItems": {
        "<TableName>": {
            "Keys": [
                {"<HashKeyName>": {"<type>":"<hash key value>"}},
                {"<HashKeyName>": {"<type>":"<hash key value>"}},
                {"<HashKeyName>": {"<type>":"<hash key value>"}}
            ]
        }
    }
}
3
votes

I believe that you're missing table name. You want this:

var params = {

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

I tried all of the solutions here and none of them worked for me, which likely means the NodeJS library has gotten an update. Referencing their better written docs, you should be able to make a request like so:

var params = {
  RequestItems: {
    'Table-1': {
      Keys: [
        {
           HashKey: 'haskey',
           NumberRangeKey: 1
        }
      ]
    },
    'Table-2': {
      Keys: [
        { foo: 'bar' },
      ]
    }
  }
};

var docClient = new AWS.DynamoDB.DocumentClient();

docClient.batchGet(params, function(err, data) {
  if (err) console.log(err);
  else console.log(data);
});

Specifically, providing the type is no longer necessary.

0
votes

Try this:

db.client.batchGetItem(
{"RequestItems":{
     "TableName":{
           "Keys":[
               {"HashKeyElement"  : {"N":"1000"}},
               {"HashKeyElement"  : {"N":"1001"}}
           ]
       }
    }
}, function(err, result){ //handle error and result here  });
0
votes

In your case, the correct answer should be:

var params = {
    "RequestItems": {
        "<table_name>": {
           "Keys": [
                {"HashKeyElement" : { "N" : "1000" } },
                {"HashKeyElement" : { "N" : "1001" } }
           ]
       }
    }
}
-1
votes

Try this, it's untested though:

var params = {
    TableName: "tableName",
    RequestItems : {
        Keys : [
            {
                HashKeyElement : { N : "1000" } 
            },
            {
                HashKeyElement : { N : "1001" } 
            }
        ]
    }
}