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.
I believe that you're missing table name. You want this:
var params = {
"RequestItems" : {
"TableName": {
"Keys" : [
{"HashKeyElement" : { "N" : "1000" } },
{"HashKeyElement" : { "N" : "1001" } }
]
}
}
}
Try this, it's untested though:
var params = {
TableName: "tableName",
RequestItems : {
Keys : [
{
HashKeyElement : { N : "1000" }
},
{
HashKeyElement : { N : "1001" }
}
]
}
}
Try this:
db.client.batchGetItem(
{"RequestItems":{
"TableName":{
"Keys":[
{"HashKeyElement" : {"N":"1000"}},
{"HashKeyElement" : {"N":"1001"}}
]
}
}
}, function(err, result){ //handle error and result here });
In your case, the correct answer should be:
var params = {
"RequestItems": {
"<table_name>": {
"Keys": [
{"HashKeyElement" : { "N" : "1000" } },
{"HashKeyElement" : { "N" : "1001" } }
]
}
}
}
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>"}}
]
}
}