使用哈希值数组的AWS DynamoDB Scan和FilterExpression

时间:2015-05-13 15:03:26

标签: javascript amazon-web-services amazon-dynamodb

我很难找到一个在DynamoDB表上使用FilterExpression进行扫描的有用示例。我在浏览器中使用javascript SDK。

我想扫描我的表并仅返回那些具有HASH字段" UID"我传递给Scan

的数组中的值

假设我有一系列唯一ID,它们是我表的哈希字段 我想从DynamoDB表中查询这些记录。

如下所示

var idsToSearch=['123','456','789'] //array of the HASH values I would like to retrieve
var tableToSearch = new AWS.DynamoDB();
var scanParams = {
  "TableName":"myAwsTable",  
  "AttributesToGet":['ID','COMMENTS','DATE'],  
  "FilterExpression":"'ID' in "+idsToSearch+"" 

}
tableToSearch.scan(scanParams), function(err,data){
    if (err) console.log(err, err.stack); //error handler
    else console.log(data); //success response
})

5 个答案:

答案 0 :(得分:12)

您应该使用IN运算符。对Placeholders使用属性名称和属性值也更容易。 但是,我建议不要在这种情况下使用Scan 。听起来您已经有了要查找的哈希键属性值,因此使用BatchGetItem会更有意义。

无论如何,这是你在Java中的表现:

ScanSpec scanSpec = new ScanSpec()
    .withFilterExpression("#idname in (:val1, :val2, :val3)")
    .withNameMap(ImmutableMap.of("#idname", "ID"))
    .withValueMap(ImmutableMap.of(":val1", "123", ":val2", "456", ":val23", "789"));
ItemCollection<ScanOutcome> = table.scan(scanSpec);

我会想象使用Javascript SDK会是这样的:

var scanParams = {
  "TableName":"myAwsTable",
  "AttributesToGet": ['ID','COMMENTS','DATE'],
  "FilterExpression": '#idname in (:val1, :val2, :val3)',
  "ExpressionAttributeNames": {
    '#idname': 'ID'
  },
  "ExpressionAttributeValues": {
    ':val1': '123',
    ':val2': '456',
    ':val3': '789'
  }
}

答案 1 :(得分:7)

我遇到了这个问题,并使用contains参数

计算出来
log

答案 2 :(得分:2)

以下是我在下面的示例中使用“scan”来获取具有特定ID(“ContentID”)的项目的方法:

var params = {
    TableName: environment.ddbContentTableName,
    ProjectionExpression: "Title, ContentId, Link",
    FilterExpression: "ContentId in (:contentId1, :contentId2, :contentId3, :contentId4),
    ExpressionAttributeValues: {":contentId1":102,":contentId2":104,":contentId3":103,":contentId4":101}
};

var docClient = new AWS.DynamoDB.DocumentClient();
docClient.scan(params, onQuery); 

然后,我可以基于已知值以编程方式构造FilterExpression和ExpressionAttributeValues,例如。

    // Create the FilterExpression and ExpressionAttributeValues
    var filterExpression =  "ContentId in ("; 

    var expressionAttributeValues = {};

    for (var i = 0; i < currentFavorites.length; i++) { 
        var contentIdName = ":contentId"+(i+1);

        if (i==0) {
            filterExpression = filterExpression + contentIdName;
        } else {
            filterExpression = filterExpression + ", " + contentIdName;
        }

        expressionAttributeValues[contentIdName] = currentFavorites[i];
    }

    filterExpression = filterExpression + ")";

    var params = {
        TableName: environment.ddbContentTableName,
        ProjectionExpression: "Title, ContentId, Link",
        FilterExpression: filterExpression,
        ExpressionAttributeValues: expressionAttributeValues
    };

    var docClient = new AWS.DynamoDB.DocumentClient();
    docClient.scan(params, onQuery); 

答案 3 :(得分:0)

我还在寻找动态解决方案,而不是手动将每个参数名称放入条件表达式中。 以下是解决方案:

List<String> valList= new ArrayList<String>(); // Populate the Values in a List    
StringJoiner valMap =  new StringJoiner(","); // This will be the dynamic Value Map

int i=1;

        table = dynamoDB.getTable(myTable);
        StringBuilder filterExpression = new StringBuilder();
        Map<String, Object> eav = new HashMap<String, Object>();

        if(!valList.isEmpty())
        {
            for(String attrVal: valList)
             {
                eav.put(":val"+i, attrVal);
                valMap.add(":val"+i);
                i++;
            }   
            filterExpression.append("attrColName in ("+valMap.toString()+")"); //here attrColName is the DB attribute
        }

        ItemCollection<ScanOutcome> items;
            items = table.scan(
                filterExpression.toString(), //FilterExpression
                null, //ProjectionExpression - choose all columns
                null, //ExpressionAttributeNames - not used in this example
                eav);//ExpressionAttributeValues 

答案 4 :(得分:0)

var params = {
TableName: "tableOne",
ProjectionExpression: "Title, ContentId, Link",
FilterExpression: "ContentId in (:contentIds)",
ExpressionAttributeValues: {":contentIds":[11,22,33,44,55]}
};

var docClient = new AWS.DynamoDB.DocumentClient();
docClient.scan(params);