【问题标题】:Function to scan AWS Dynamo DB recursively for Nodejs递归扫描 AWS Dynamo DB 以查找 Nodejs 的函数
【发布时间】:2017-04-10 03:50:49
【问题描述】:

所以我需要node.js 中的递归函数来替换这个函数调用:

docClient.scan(params, callback)

更多信息见http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.04.html

【问题讨论】:

  • 您有问题吗?
  • 已经回答,谢谢

标签: node.js asynchronous recursion amazon-dynamodb


【解决方案1】:

这是在LastEvaluatedKey 可用之前执行扫描的递归代码。

var AWS = require("aws-sdk");
var creds = new AWS.Credentials('akid', 'secret', 'session');

AWS.config.update({
    region: "us-west-2",
    endpoint: "http://localhost:8000",
    credentials : creds
});

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

var params = {
    TableName: "Movies"
};

console.log("Scanning Movies table.");
docClient.scan(params, onScan);
var count = 0;

function onScan(err, data) {
    if (err) {
        console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        // print all the movies
        console.log("Scan succeeded.");
        data.Items.forEach(function(movie) {
           console.log("Item :", ++count,JSON.stringify(movie));
        });

        // continue scanning if we have more movies
        if (typeof data.LastEvaluatedKey != "undefined") {
            console.log("Scanning for more...");
            params.ExclusiveStartKey = data.LastEvaluatedKey;
            docClient.scan(params, onScan);
        }
    }
}  

【讨论】:

  • 太棒了!但是应该需要添加结果的计数,这不是那么便携,因为它正在记录但不返回值。
猜你喜欢
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 2015-12-04
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
相关资源
最近更新 更多