如何使用Lambda AWS从Amazon DynamoDB获取行数

时间:2015-06-10 03:24:14

标签: amazon-dynamodb aws-lambda

我的Lambda脚本是:

var AWS = require("aws-sdk");
var DOC = require("dynamodb-doc");
AWS.config.update({region: "us-west-1"});
var docClient = new DOC.DynamoDB();
var dynamodb = new AWS.DynamoDB();

exports.handler = function(event, context) {
    var params = {};
    params.TableName = "Emails";
    params.ConditionalOperator = "AND";
    params.ScanFilter = {
        Machinekey:{
            ComparisonOperator:"EQ",
            AttributeValueList: [{S: "okok"}]
        }
    };
    params.Select= "COUNT";

    //params.IndexName = "Machinekey-index";
    //params.KeyConditions = docClient.Condition("EmailId", "NOT_NULL");
    //params.QueryFilter = docClient.Condition("Machinekey", "EQ","ARUZE-010415_15112442NLHIYTLY-A23V3.9");
    dynamodb.scan(params, function(err, data) { 
    if (err) {
        console.log(err, err.stack);
    } else {
        //console.log(data);
        context.succeed(data.Count);
    }
});
};

执行日志的结果:

[ValidationException:ConditionalOperator只能在Filter或Expected有两个或更多元素时使用]   消息:' ConditionalOperator只能在Filter或Expected有两个或多个元素时使用',   代码:' ValidationException',   时间:2015年6月10日星期三03:12:34 GMT + 0000(UTC),   statusCode:400,   可重试:false,

拜托,帮助我!

1 个答案:

答案 0 :(得分:0)

您指定ConditionalOperator = "AND";,但是当前查询只包含一个条件(Machinekey ==" okok")时,您不需要这样做。如果您有两个或更多条件,则只需要ConditionalOperator

尝试评论ConditionalOperator并再次尝试:

var params = {};
params.TableName = "Emails";
//params.ConditionalOperator = "AND";
params.ScanFilter = {
    Machinekey:{
        ComparisonOperator:"EQ",
        AttributeValueList: [{S: "okok"}]
    }
};
params.Select= "COUNT";
...