【问题标题】:How to use 'BatchGetItem' for the NodeJS AWS-SDK for DynamoDB如何将“BatchGetItem”用于 NodeJS AWS-SDK for DynamoDB
【发布时间】:2013-02-09 04:25:27
【问题描述】:

我正在尝试使用 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。谢谢!!!
  • 对于在浏览器中使用 js 来这里的任何人,请注意您需要 batchGet 而不是 batchGetItem

标签: node.js


【解决方案1】:

我感受到你的痛苦……AWS 文档充其量是令人困惑的。我认为这是由老化的基础设施和糟糕的技术写作造成的。 SDK 使用的 nodejs 和 JSON 语法让我想起了 XML 结构。

无论如何,我设法让 BatchGetItem 在一个小时后工作。参数应如下所示:

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

【讨论】:

    【解决方案2】:

    我在这里尝试了所有解决方案,但没有一个对我有用,这可能意味着 NodeJS 库已得到更新。参考他们写得更好的docs,你应该可以提出这样的请求:

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

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

    【讨论】:

    • 这是因为有些用户使用的是普通的dynamoDb客户端,你使用的是documentClient
    【解决方案3】:

    我使用的是 dynamo db 客户端版本……经过一个小时的研究,我设法让它工作了……

    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 并得到“未捕获的 TypeError:docClient.batchGetItem 不是函数”。在文档中找不到这个。
    • @GrahamHesketh documentclient 的 batchget 通过委托给 AWS.DynamoDB.batchGetItem() 从一个或多个表中返回一个或多个项目的属性。
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      试试这个:

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

      【讨论】:

      • 不适合我...你能粘贴一个有效的样本吗? :) 非常感谢!
      • 假设如果你的 tableName 是'Cluster',你可以写--pastebin.com/5eVaVALQ 如果你仍然遇到问题,给我看你的代码sn-p。
      • 不,它不能那样工作。请参阅下面的答案
      • 它对我有用,你的答案和其他人都一样。
      【解决方案6】:

      试试这个,虽然它未经测试:

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

      【讨论】:

        【解决方案7】:

        我相信您缺少表名。你想要这个:

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

        【讨论】:

          猜你喜欢
          • 2019-11-15
          • 1970-01-01
          • 2016-04-10
          • 1970-01-01
          • 2022-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-01
          相关资源
          最近更新 更多