如何使用NodeJS AWS-SDK的BatchGetItem来操作DynamoDB?

15
我正在尝试使用Node JS AWS-SDK从DynamoDB表中获取项目。函数getItem运行良好,但使用BatchGetItem较为困难。
我使用了官方文档:http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/Client.html#batchGetItem-property 我正在寻找如何正确使用此函数的示例,但是我找不到任何示例。我编写的代码如下:
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));
});

我遇到了一个SerializationException: Start of list found where not expected错误,但就我的NodeJS和JSON专业知识而言,我的语法是正确的。但这很令人困惑:http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/API_BatchGetItems.html 在该语法示例中,您必须提供表名。

你好,你的语法有没有成功运行呢?能否分享一下你的解决方案?谢谢!或许你可以将getItem和getBatchItem粘贴到pastebin上。谢谢!!! - Cmag
对于在浏览器中使用js的任何人,请注意您需要使用batchGet而不是batchGetItem。 - Graham Hesketh
7个回答

18

我使用的是 DynamoDB 客户端版本... 经过一个小时的研究,我成功让它工作了...

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

});

所以对于浏览器中的js,似乎需要使用batchGet,但是对于node,则需要使用batchGetItem。我尝试从浏览器中使用batchGetItem,并得到了“Uncaught TypeError: docClient.batchGetItem is not a function”的错误。在文档中找不到这个问题的解决方法。 - Graham Hesketh
@GrahamHesketh,DocumentClient的batchget通过委托给AWS.DynamoDB.batchGetItem()从一个或多个表中返回一个或多个项的属性。 - Dmitry Grinko

11
我理解你的痛苦... AWS文档最多只能算是令人困惑。我认为这是由于老化的基础设施和糟糕的技术写作所导致的。SDK使用的nodejs和JSON语法让我想起了XML结构。
无论如何,经过一个小时的努力,我终于成功让BatchGetItem运行起来了。参数应该像下面这样:
{
    "RequestItems": {
        "<TableName>": {
            "Keys": [
                {"<HashKeyName>": {"<type>":"<hash key value>"}},
                {"<HashKeyName>": {"<type>":"<hash key value>"}},
                {"<HashKeyName>": {"<type>":"<hash key value>"}}
            ]
        }
    }
}

4

我认为您缺少表名。您需要这样做:

var params = {

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

2

我尝试了这里的所有解决方案,但都没有成功,这很可能是因为NodeJS库已经更新了。参考他们写得更好的文档,您应该能够像这样发出请求:

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

具体来说,不再需要提供类型。

1
这��因为一些用户正在使用普通的dynamoDb客户端,而您正在使用documentClient。 - Matteo

0

试试这个:

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

对我不起作用...你能否请在pastebin上分享一个可行的示例? :) 非常感谢! - Cmag
假设您的表名为“Cluster”,您可以编写-- http://pastebin.com/5eVaVALQ 如果您仍然遇到问题,请向我展示您的代码片段。 - Danish
不,它不是那样工作的。请看下面我的答案。 - DaHoopster
对我来说,这个方法有效,并且你的答案与其他人的类似。 - Danish

0

在您的情况下,正确答案应该是:

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

-1

你可以试试这个,不过还没有经过测试:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接