带字符串集的Android DynamoDB过滤表达式 [英] Android DynamoDB Filtering Expression with String Set

查看:53
本文介绍了带字符串集的Android DynamoDB过滤表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用字符串集过滤查询结果。我的字符串集由国家/地区组成。

I'm trying to filter the results of a query using a String Set. My String set is made up of countries.

基本上,执行查询后,我希望过滤器查看字符串集的内容,然后仅显示结果字符串集中有谁的国家。这可能吗?这是我的代码:

Basically, after performing the query, I want the filter to look at the contents of the string set and then only display the results who's countries are in the String Set. Is this possible? Here is the code I have:

String[] countries = { "Japan", "Vietnam", "Thailand"};
Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
eav.put(":val2", new AttributeValue().withSS(countries));

DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression()
    .withFilterExpression("Place IN (:val2)") // this is the line i'm confused about
    .withExpressionAttributeValues(eav)
    .withHashKeyValues(someHashValue);

// Results of query should only show those with Place = Japan, Vietnam, or Thailand

Place是我要检查的列的名称。

Where "Place" is the name of the column I'm trying to check.

我知道这样做的一种方法是使用多个OR语句去检查。但是,我希望该过滤器与任何String设置大小兼容,因此似乎OR语句并不是可行的方法。

I know one method of doing this is to use multiple OR statements to check. However, I want this filter to be compatible with any String set size, so it seems like OR statements aren't the way to go.

我非常感谢任何指导。

I appreciate any guidance at all. Thanks in advance!

推荐答案

一件事-StringSet是DynamoDB项目字段的值类型,不是您所需要的东西可以一次查询多个事物。

One thing - a StringSet is a value type for a field of a DynamoDB item, and not something you can use to query for multiple things at once.

我四处张望,发现一些可以完美地与您的设计配合使用的方法,但已过时了。我建议您自己构建查询字符串。例如...

I looked around a bit and found some methods that would work elegantly with your design, but they're deprecated. What I would recommend is building up the query string yourself. For example...

String[] countries = {"Japan", "Vietnam", "Thailand"};

List<String> queries = new ArrayList<>();
Map<String, AttributeValue> eav = new HashMap<>();

for (Integer i = 0; i < countries.length; i++) {
    String placeholder = ":val" + i;
    eav.put(placeholder, new AttributeValue().withS(countries[i]));
    queries.add("Place = " + placeholder);
}

String query = String.join(" or ", queries);

DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression()
    .withFilterExpression(query)
    .withExpressionAttributeValues(eav)
    .withHashKeyValues(someHashValue);

我还没有测试过,但这是您要考虑的事情(除非有更好的方法,我仍然没有看到)。

I haven't tested it, but this is the sort of thing you'd be looking at doing (unless there's a better way that I still haven't seen).

这篇关于带字符串集的Android DynamoDB过滤表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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