用于递归扫描AWS Dynamo DB以查找Nodej的功能

时间:2016-11-25 17:42:09

标签: node.js asynchronous recursion amazon-dynamodb

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

docClient.scan(params, callback)

更多信息,请参阅http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.04.html

1 个答案:

答案 0 :(得分:4)

以下是在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);
        }
    }
}