首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DynamoDB扫描返回多个扫描结果

DynamoDB扫描返回多个扫描结果
EN

Stack Overflow用户
提问于 2020-03-19 20:37:36
回答 2查看 810关注 0票数 0

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

该函数基本上接受来自API调用的参数列表,查询每个参数的db,然后返回结果。

我发现扫描在一个参数下运行得很好,但是当超过一个参数被调用时返回重复的数据。从日志中我可以看到,当传递多个参数时,扫描会多次运行。

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

代码语言:javascript
复制
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

对于两个参数,日志是

代码语言:javascript
复制
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个params,原木是

代码语言:javascript
复制
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循环和扫描的代码。我对参数进行了硬编码,排除了一些无关的内容。

代码语言:javascript
复制
     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循环运行的次数不超过应该运行的次数,那么为什么要调用搜索函数呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-20 14:03:04

下面是一个示例,说明如何使用DynamoDB DocumentClient按分区键查询多个项并收集结果。这使用了query()调用的Promisified变体,并等待使用Promise.all()实现所有查询承诺。

代码语言:javascript
复制
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]);
  }
})();
票数 1
EN

Stack Overflow用户

发布于 2020-03-19 22:29:46

每当您想在DynamoDB数据库中搜索某些内容时,建议您使用查询选项,而不是Scan

这是因为扫描读取数据库的每一项,而查询只查找提到的Hask键(主键)。

如果您想要查找具有特定“属性”的数据,可以使用Global备用索引,其中可以将“属性”设置为Hash键,同时可以选择一种您选择的键。这可能解决表多次返回答案的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60764689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档