-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathscan-parallel-segments.js
35 lines (31 loc) · 1.13 KB
/
scan-parallel-segments.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const AWS = require("aws-sdk");
AWS.config.update({ region: "us-west-2" });
const documentClient = new AWS.DynamoDB.DocumentClient();
const segments = 5;
// Define a total number segments to scan across, and execute multiple table scans asynchronously.
async function parallelScan(segments = 1) {
let results = [];
for (let i = 0; i < segments; i++) {
let scan = documentClient
.scan({
TableName: "Products",
FilterExpression: "#status = :status",
ExpressionAttributeNames: {
"#status": "ProductStatus",
},
ExpressionAttributeValues: {
":status": "IN_STOCK",
},
TotalSegments: segments, // The total number of segments to scan across, in this case 5.
Segment: i, // The segment index for this particular query, zero indexed, in this case 0-4.
})
.promise();
// Push unsettled scan Promise into results array.
results.push(scan);
}
// Use Promise.all to return all scan results as a single Promise.
return Promise.all(results);
}
parallelScan(segments)
.then((scans) => console.log("ParallelScan succeeded:", scans.length))
.catch((error) => console.error(JSON.stringify(error, null, 2)));