简体   繁体   English

在Java中使用包含过滤器的DynamoDB扫描

[英]Using contains filter in DynamoDB scan with Java

Suppose I have a working query such as: 假设我有一个有效的查询,例如:

ScanRequest scanRequest = new ScanRequest()
            .withTableName("myTable")
            .withFilterExpression("attr1 = :val1 and attr2 = :val2")
            .withExpressionAttributeValues(vm)  //contains values for :val1 and :val2
            .withLimit(PAGE_SIZE)
            .withConsistentRead(READ_TYPE);

Now I would like to extend this scan. 现在我想扩展这个扫描。 Suppose my table also has an attribute attr3 which has the form: 假设我的表还有一个attr3属性,其格式如下:

"attr3": {
    "S": "AAA BBB CCC DDD"
}

How can I filter elements who's attr3 contains AAA? 如何过滤attr3包含AAA的元素? Or AAA and BBB? 还是AAA和BBB?

The DynamoDB Condition Expressions Reference documentation is your friend! DynamoDB 条件表达式参考文档是您的朋友!

In your specific case you can use the contains function to search for a sub-string. 在您的特定情况下,您可以使用contains函数来搜索子字符串。 Your filter expression might look something like this: 您的过滤器表达式可能如下所示:

"attr1 = :val1 and attr2 = :val2 and (contains(attr3, :val3a) or contains(attr3, :val3b))"             
// where :val3a and :val3b are value placeholders for say AAA and BBB

But I suspect that what you want to achieve is a bit more sophisticated than can be handled server-side by DynamoDB filters, so you have two options: 但我怀疑你想要实现的东西比DynamoDB过滤器在服务器端处理的要复杂一些,所以你有两个选择:

  1. do the filtering in the application logic (ie. bring down your results to the client and filter there), or; 在应用程序逻辑中进行过滤(即将结果记录到客户端并在那里过滤),或者;
  2. change your attribute from a string to a list, or string set (if duplicates not allowed) 将您的属性从字符串更改为列表或字符串集(如果不允许重复)

In either case, you should know that a scan with filters is only more efficient than a regular scan in terms of network band-with (in the case when most results are excluded by the filter). 在任何一种情况下,您都应该知道,在网络带宽方面,使用过滤器的扫描仅比常规扫描更有效(在过滤器排除大多数结果的情况下)。 But in terms of capacity consumed a Scan is equally expensive with or without filters. 但就容量消耗而言,无论是否使用过滤器,扫描都同样昂贵。 So if you can avoid it, don't rely on scanning your table too much! 所以,如果你可以避免它,不要过分依赖扫描你的桌子!

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

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