请使用dynamodb和nodejs指导正确的脚本以获得动态结果

时间:2017-03-31 11:24:13

标签: amazon-dynamodb

通过编写下面的代码,我一次又一次地编写脚本以获得添加的记录,而不是一次又一次地编写脚本我希望在结果中添加记录。使用dynamodb和nodejs。请提供正确的脚本。

var AWS = require("aws-sdk");
var async = require("async");

AWS.config.update({
      region: "us-west-2",
      endpoint: "2345",
      accessKeyId: "A",
      secretAccessKey:"x"
    });

var db = new AWS.DynamoDB.DocumentClient()

var table = "rets_property_all";
var pstart =new Date () .getTime ();

async.parallel({
        0 : function(callback){
        db.scan ({TableName: table,
        ProjectionExpression: "#cityname,ListingKey ",
        FilterExpression: "#cityname = :v_id",
        ExpressionAttributeNames: {
        "#cityname": "CityName",
          },
          ExpressionAttributeValues: {":v_id" : 'BALTIMORE'},
            TotalSegments: 2,
            Segment:  0//by the worker who has been called
        },function (err , res) {
            callback (null , res.Items);
        });
            },
            1 : function(callback){
        db.scan ({TableName: table,
          ProjectionExpression: "#cityname,ListingKey ",
          FilterExpression: "#cityname = :v_id",
          ExpressionAttributeNames: {
              "#cityname": "CityName",
          },
          ExpressionAttributeValues: {":v_id" : 'BALTIMORE'},
            TotalSegments: 2,
            Segment: 1//by the worker who has been called
        }, function(err,results){
      if (err) {throw err; }
      var pend = new Date () .getTime ();

      console.log (results);
        })

1 个答案:

答案 0 :(得分:0)

以下是使用async.parallel的代码。

一次性获取所有3000件物品所需的段数取决于物品的大小。您可能需要计算整个数据的大小以确定段的数量。您可以保留段的最大数量。如果没有找到特定段扫描的数据,DynamoDB不会抛出错误。

var scanFunctions = [];
var totalSegments = 11;
var segmentNos = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

segmentNos.forEach(function (segmentNo)  {
    console.log(segmentNo);
    var scanFunction = function (callback) {
        console.log("scanning ..." + segmentNo);
        db.scan({
            TableName: tableName,
ProjectionExpression: "#cityname,ListingKey ",
    FilterExpression: "#cityname = :v_id",
    ExpressionAttributeNames: {
    "#cityname": "CityName",
      },
      ExpressionAttributeValues: {":v_id" : 'BALTIMORE'},
            Segment: segmentNo,
            TotalSegments: totalSegments                  
        }, function (err, res) {
            if (err) {
                console.log("Error ===>" + JSON.stringify(err));
            } else {
                console.log("SegmentNo ===>" + segmentNo + "   ;Data ===>" + JSON.stringify(res));
            }
            callback(err, res.Items);

        });

    }
    console.log('pushing scan funs to array....');
    scanFunctions.push(scanFunction);
});

async.parallel(scanFunctions, function (err, results) {
    console.log("Results ===>" + JSON.stringify(results, null, 4));
});