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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36620571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:29:28  来源:igfitidea点击:

Using contains filter in DynamoDB scan with Java

javaamazon-web-servicesfilteramazon-dynamodbcontains

提问by user3146897

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? Or AAA and BBB?

如何过滤 attr3 包含 AAA 的元素?还是AAA和BBB?

采纳答案by Mike Dinescu

The DynamoDB Condition Expressions Reference documentationis your friend!

DynamoDB条件表达式参考文档是您的朋友!

In your specific case you can use the containsfunction to search for a sub-string. Your filter expression might look something like this:

在您的特定情况下,您可以使用contains函数来搜索子字符串。您的过滤器表达式可能如下所示:

"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)
  1. 在应用程序逻辑中进行过滤(即,将您的结果提交给客户端并在那里进行过滤),或者;
  2. 将您的属性从字符串更改为列表或字符串集(如果不允许重复)

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!

无论哪种情况,您都应该知道使用过滤器的扫描仅在网络带宽方面比常规扫描更有效(在大多数结果被过滤器排除的情况下)。但就消耗的容量而言,无论是否使用过滤器,扫描都同样昂贵。因此,如果您可以避免它,请不要过分依赖扫描您的桌子!