所以我写了下面的函数。这个版本有点删节,我已经匿名的数据,但关键的组成部分在那里。
该函数基本上接受来自API调用的参数列表,查询每个参数的db,然后返回结果。
我发现扫描在一个参数下运行得很好,但是当超过一个参数被调用时返回重复的数据。从日志中我可以看到,当传递多个参数时,扫描会多次运行。
例如,使用一个param函数日志返回
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个params,原木是
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循环运行的次数不超过应该运行的次数,那么为什么要调用搜索函数呢?
发布于 2020-03-20 14:03:04
下面是一个示例,说明如何使用DynamoDB DocumentClient按分区键查询多个项并收集结果。这使用了query()调用的Promisified变体,并等待使用Promise.all()实现所有查询承诺。
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]);
}
})();发布于 2020-03-19 22:29:46
每当您想在DynamoDB数据库中搜索某些内容时,建议您使用查询选项,而不是Scan
这是因为扫描读取数据库的每一项,而查询只查找提到的Hask键(主键)。
如果您想要查找具有特定“属性”的数据,可以使用Global备用索引,其中可以将“属性”设置为Hash键,同时可以选择一种您选择的键。这可能解决表多次返回答案的问题。
https://stackoverflow.com/questions/60764689
复制相似问题