使用哈希值数组的AWS DynamoDB Scan和FilterExpression [英] AWS DynamoDB Scan and FilterExpression using array of hash values

查看:79
本文介绍了使用哈希值数组的AWS DynamoDB Scan和FilterExpression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a hard time finding a useful example for a scan with FilterExpression on a DynamoDB table. I am using the javascript SDK in the browser.

我想扫描我的表并仅返回那些在传递给Scan的数组中具有HASH字段UID值的记录

I would like to scan my table and return only those records that have HASH field "UID" values within an array I pass to the Scan

假设我有一组唯一的id是我表的哈希字段
我想从DynamoDB表中查询这些记录。

Lets say I have an array of unique ids that are the hash field of my table I would like to query these records from my DynamoDB table.

如下所示

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
})


推荐答案

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

You should make use of the IN operator. It is also easier to use Placeholders for attribute names and attribute values. I would, however, advise against using a Scan in this case. It sounds like you already have the hash key attribute values that you want to find, so it would make more sense to use BatchGetItem.

无论如何,这是你要怎么做它在Java中:

Anyways, here is how you would do it in 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会是这样的:

I would imagine using the Javascript SDK it would be something like this:

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

这篇关于使用哈希值数组的AWS DynamoDB Scan和FilterExpression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆