简体   繁体   English

带有密钥的AWS DynamoDB Lambda扫描

[英]AWS DynamoDB Lambda Scan with key

I am currently making a lambda scan where I need to scan a table which currently is bigger than 1mb (the size limit of a single scan). 我目前正在进行lambda扫描,需要扫描当前大于1mb(单次扫描的大小限制)的表。 However, my current experience doesnt allow me to rewrite the code for repetitive actions. 但是,根据我目前的经验,我无法为重复性操作重写代码。 This is the current call: 这是当前调用:

 const params = {
  TableName: 'product',
};
let items = []
try {
  const result = await dynamoDbLib.call("scan", params);
  items = result.Items;
  if (result.LastEvaluatedKey) {
    params.ExclusiveStartKey = result.LastEvaluatedKey;
    const newResult = await dynamoDbLib.call("scan", params);
    items.concat(newResult.Items)
  }
  callback(null, success(items));
} catch (e) {
  callback(null, failure({ status: false }));
}

So it fires two times, but it should keep on going until the last key is undefined. 因此它会触发两次,但应该继续进行直到未定义最后一个键。 Any help would be much appreciated! 任何帮助将非常感激!

Greetings Bram 问候布拉姆

Create a recursive function that will stop when the LastEvaluatedKey is undefined. 创建一个递归函数 ,该LastEvaluatedKey在未定义LastEvaluatedKey时将停止。 It will keep on collecting the results of the scan until the LastEvaluatedKey is undefined and then it will return you the results of the scan. 它将继续收集扫描结果,直到未定义LastEvaluatedKey为止,然后它将返回扫描结果。

async function scanTillEnd(params, items){
     const result = await dynamoDbLib.call("scan", params);
     items.concat(result.Items)
     //check if they are more items to get
     if (result.LastEvaluatedKey) {
       params.ExclusiveStartKey = result.LastEvaluatedKey;
       //scan the table again
       return await scanTillEnd(params, items)
    }
    else
       //have scanned till the end, return the results
       return items
}
const params = {
    TableName: 'product',
};
let items = []
let results = scanTillEnd(params,items)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM