AWS Lambda NodeJS:使用可选过滤器属性进行扫描

时间:2019-02-21 10:48:00

标签: node.js amazon-web-services aws-lambda amazon-dynamodb dynamodb-queries

我构建了一个AWS Lambda,它可以扫描和过滤DynamoDB并返回到我的AWS API。因此,我有三个可能的搜索参数(项目名称,作者和类型),但我不知道在查询中使用了哪些参数。首先,我实现了一个版本,其中所有搜索参数都进行了硬编码。结果,如果不是所有定义的搜索参数,我都会报错。最后,我重新编写了代码,以根据输入的搜索参数构建单独的扫描参数。

代码工作正常,但是我认为对于此问题有更好的实现,也许您可​​以给我一些改进建议。否则,这将帮助面临相同问题的人们使用可选的搜索参数。

var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB.DocumentClient();

//This is the Lambda function
exports.handler = function(event, context, callback) 
{
//In case we query without query attributes
if(!event.hasOwnProperty("queryStringParameters"))
        {
            console.log("NO queryStringParameters FOUND");
            var emptyparams = 
                {  
                    TableName: "blackboard-items",
                };
            docClient.scan(emptyparams, onScan);
            return;
        }
        
//we want to tailor this attributes for the params for docClient.scan(params, onScan); 
var queryParam = event["queryStringParameters"];
var filterexpression = "";
var expressionAttributeNames = {}; //Instantiate
var expressionAttributeValues = {};
console.log("QUERY PARAMETERS: " + JSON.stringify(queryParam));
    
//Do we look for an author?
if(queryParam.hasOwnProperty("author"))
    {
        console.log("FOUND AUTHOR");
        filterexpression += "contains(#author, :author)"; //Collect scan params
        expressionAttributeNames['#author'] = 'author';
        expressionAttributeValues[':author'] = event["queryStringParameters"]["author"];
    }
    
//Do we look for an itemname?
if(queryParam.hasOwnProperty("itemname"))
    {
    console.log("FOUND ITEMNAME");
    if(filterexpression !== "")
        filterexpression += " AND contains(#itemname, :itemname)";
    else
        filterexpression += "contains(#itemname, :itemname)";
        
    expressionAttributeNames['#itemname'] = 'itemname';
    expressionAttributeValues[':itemname'] = queryParam["itemname"];    
    }
    
//Do we look for a type?
if(queryParam.hasOwnProperty("type"))
    {
    console.log("FOUND TYPE");
    if(filterexpression !== "")
        filterexpression += " AND #type = :type";
    else
        filterexpression += "#type = :type";
        
    expressionAttributeNames['#type'] = 'type';
    expressionAttributeValues[':type'] = event["queryStringParameters"]["type"]; 
    }
  
//Build params based on the tailored parts
var params = 
    {  
        TableName: "blackboard-items",
        FilterExpression: filterexpression,
        ExpressionAttributeNames: expressionAttributeNames,
        ExpressionAttributeValues: expressionAttributeValues,
    };
  
//Use tailored params for scan()
docClient.scan(params, onScan);
var count = 0;

function onScan(err, data)
    {
       if (err) 
        {
        console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
        } 
        else 
        {        
        console.log("Scan succeeded.");
        data.Items.forEach(function(itemdata) 
            {
               console.log("Item :", ++count,JSON.stringify(itemdata));
            });

            // continue scanning if we have more items
            if (typeof data.LastEvaluatedKey != "undefined") 
            {
                console.log("Scanning for more...");
                params.ExclusiveStartKey = data.LastEvaluatedKey;
                docClient.scan(params, onScan);
            }
        }
        
        
    var response = 
    {
    "isBase64Encoded": false,
    "statusCode": "200",
    "headers": {  },
    "body": JSON.stringify(data.Items)
    };
 
    callback(null, response);
        
    }
 };

注意: 数据库的主键是“ itemname”,但是我将尽快重新设计数据库设计以具有排序键。

1 个答案:

答案 0 :(得分:0)

DynamoDB的查询功能非常有限,因此,您应该不惜一切代价avoid scan。每次扫描操作都会消耗RCU读取的每个项目。如果您的表中有很多项目,它会很快用完RCU。

如果要通过这3个属性进行查询,则DynamoDB可能不是适合您的用例的最佳数据库。如果可以一次将查询范围缩小到1个属性,则可以使用Global Secondary Indexes。这样,您可以根据作者或类型进行查询。您可以查看this答案,了解如何在DynamoDB中查询GSI