javascript - DynamoDB 扫描返回多个扫描结果

标签 javascript node.js amazon-web-services asynchronous aws-lambda

所以我写了下面的函数。这个版本有一点删节,我已经匿名了数据,但关键组件都在那里。

该函数基本上从 API 网关调用中获取参数列表,查询每个参数的数据库,然后返回结果。

我发现扫描可以通过一个参数完美运行,但当调用超过 1 个参数时会返回重复数据。从日志中我可以看到,当传递多个参数时,扫描会运行多次

例如,使用一个参数,函数日志将返回

2020-03-19 20:27:42.974 Starting the 0 scan with 3 as the id 
2020-03-19 20:27:43.047 The 0 scan has completed successfully

有两个参数的日志是

2020-03-19 20:28:42.189 Starting the 0 scan with 2 as the id
2020-03-19 20:28:42.261 The 0 scan has completed successfully
2020-03-19 20:28:42.262 Starting the 1 scan with 3 as the id
2020-03-19 20:28:42.267 The 0 scan has completed successfully
2020-03-19 20:28:42.293 The 1 scan has completed successfully

日志有 3 个参数

2020-03-19 20:29:49.209 Starting the 0 scan with 1 as the id
2020-03-19 20:29:49.323 The 0 scan has completed successfully
2020-03-19 20:29:49.325 Starting the 1 scan with 2 as the id
2020-03-19 20:29:49.329 The 0 scan has completed successfully
2020-03-19 20:29:49.380 The 1 scan has completed successfully
2020-03-19 20:29:49.381 Starting the 2 scan with 3 as the id
2020-03-19 20:29:49.385 The 1 scan has completed successfully
2020-03-19 20:29:49.437 The 2 scan has completed successfully

这是运行 for 循环和扫描的代码。我已经对参数进行了硬编码并排除了一些不相关的内容

     const params = ['1','2','3'];
     for (let i = 0; i < params.length; i++) {
      console.log("Starting the " + i + " scan with " + params[i] + " as the scan parameter")
      const scanParams = {
      TableName: "Dynamo_Table",
      FilterExpression: "Org = :Org",
      ExpressionAttributeValues: { ":Org": params[i] },
      ProjectionExpression: "User_ID, Org, first_name, last_name"
     };
     await dynamoClient.scan(scanParams, function(err, data) {
      if (err) {
        console.log("data retrival failed, error logged is :" + err);
        return err;
      }
      else {
        console.log("The " + i +" scan has completed successfully")
        //console.log("data retrival successful: " + JSON.stringify(data));
        userData = userData.concat(data.Items)
        //console.log("partial data structure is " + data)
      }
    }).promise();
  }
      responseData = JSON.stringify(userData)
      console.log("Complete response is " + responseData)
      console.log("data after execution scan is " + data)

我尝试通过定义等待并使用 AWS 的 .promise() 函数来强制程序等待扫描竞争。然而,这些似乎并没有阻止线程执行。我不确定为什么它会启动多次扫描。 for 循环运行的次数并没有超出应有的次数,那么为什么会调用搜索函数呢?

最佳答案

以下示例演示了如何使用 DynamoDB DocumentClient 按分区键查询多个项目并收集结果。这使用 query() 调用的 Promisified 变体,并使用 Promise.all() 等待所有查询 promise 得到满足。

var AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });

const dc = new AWS.DynamoDB.DocumentClient();

// Array of organization IDs we want to query
const orgs = ['1', '2', '3'];

// Async function to query for one specific organization ID
const queryOrg = async org => {
  const params = {
    TableName: 'orgs',
    KeyConditionExpression: 'org = :o1',
    ExpressionAttributeValues: { ':o1': org, },
  };

  return dc.query(params).promise();
}

// Async IIFE because you cannot use await outside of an async function
(async () => {
  // Array of promises representing async organization queries made
  const promises = orgs.map(org => queryOrg(org));

  // Wait for all queries to complete and collect the results in an array
  const items = await Promise.all(promises);

  // Results are present in the same order that the queries were mapped
  for (const item of items) {
    console.log('Item:', item.Items[0]);
  }
})();

关于javascript - DynamoDB 扫描返回多个扫描结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60764689/

相关文章:

javascript - 添加为属性时如何访问另一个函数中的函数?

node.js - Express 中的 Laravel 队列作业等价于什么?

node.js - 我在其中创建或修改 View 的全局变量(ejs)

php - AJAX 调用的 Yii SQL 分析

javascript - jquery 验证错误 预期的标识符、字符串或数字

javascript - Firefox 的 getBoundingClientRect 问题

amazon-web-services - 动态访问主机端口并将其设置为aws中的DockerLabels

apache - 如何在 Apache 上配置子域以指向另一个 Web 应用程序或目录;后端有tomcat

javascript - Oclick - 更改元素的背景图像

node.js - Sails.js 使用参数获取路由的 href