Skip to content

Instantly share code, notes, and snippets.

@daverickdunn
Last active April 21, 2020 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daverickdunn/b7af280a5a880a18e7f9fae5b89e5d61 to your computer and use it in GitHub Desktop.
Save daverickdunn/b7af280a5a880a18e7f9fae5b89e5d61 to your computer and use it in GitHub Desktop.
DynamoDB - Dynamically Map-Reduce a Batch Update Request in to Blocks of 25 Items
// Your DocClient ...
function SplitBatch(params) {
const max_items = 25;
let tables = Object.entries(params.RequestItems);
let transactions = {};
for (const [table, requests] of tables) {
for (let index = 0; index < requests.length; index += max_items) {
let block = requests.slice(index, index + max_items);
if (!transactions[table]) {
transactions[table] = [block]
} else {
let last = transactions[table][transactions[table].length - 1];
let idx = last.length;
if(idx < max_items){
let first = block.slice(0, idx);
let second = block.slice(idx);
last.splice(idx, 0, ...first);
transactions[table].push(second)
} else {
transactions[table].push(block)
}
}
}
}
let reduced = Object.entries(transactions)
.reduce((acc, [table, requests]) => {
for (const req of requests){
acc.push({
RequestItems: {
[table]: req
}
});
}
return acc;
}, [])
return reduced;
}
async function PutBatch(items) {
var params = {
RequestItems: {
'tablename': items.map(item => ({
PutRequest: {
Item: item
}
}))
}
};
let requests = SplitBatch(params);
let unprocessed = []
for (const req of requests) {
await DocClient.batchWrite(req).promise()
.then(
item => unprocessed.splice(unprocessed.length, item['UnprocessedItems']),
err => console.log(err, params)
);
}
return unprocessed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment