Java DynamoDB:如何使用查询过滤器检查 MAP 中的条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30143838/
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
DynamoDB: How to use a query filter to check for conditions in a MAP
提问by ThePedestrian
I have a table and the structure looks like this:
我有一张表,其结构如下所示:
When I do a query, I would like to be able to do a query filter on the data map; but I'm not exactly sure how to setup the query.
当我做查询时,我希望能够对数据图进行查询过滤;但我不确定如何设置查询。
This is what I have so far:
这是我到目前为止:
HashMap<String, AttributeValue> map = new HashMap<String, AttributeValue>();
map.put("byUserId", new AttributeValue().withS("vl49uga5ljjcoln65rcaspmg8u"));
queryExpression
.withQueryFilterEntry("data", new Condition()
.withAttributeValueList(new AttributeValue().withM(map))
.withComparisonOperator(ComparisonOperator.CONTAINS));
but the way I'm building the filter is not correct and I keep running into the following error:
但是我构建过滤器的方式不正确,我不断遇到以下错误:
Exception in thread "main" com.amazonaws.AmazonServiceException: One or more parameter values were invalid: ComparisonOperator CONTAINS is not valid for M AttributeValue type (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: CHIOME68L1HVGO81URD7CIOS6BVV4KQNSO5AEMVJF66Q9ASUAAJG)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1077)
at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:725)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:460)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:295)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:3106)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.query(AmazonDynamoDBClient.java:1118)
So what is a comparison operator I should be using (since IN is for list types), and how do I build the query filter such that I can specify a comparison inside of the MAP.
那么我应该使用什么比较运算符(因为 IN 用于列表类型),以及如何构建查询过滤器以便我可以在 MAP 内部指定比较。
Thanks!
谢谢!
采纳答案by bsd
Try comparing the map attribute data.byUserId
instead of the entire map structure:
尝试比较地图属性data.byUserId
而不是整个地图结构:
Table table = dynamoDB.getTable(tableName);
Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
expressionAttributeValues.put(":x", "vl49uga5ljjcoln65rcaspmg8u");
QuerySpec spec = new QuerySpec()
.withHashKey("HashKeyAttribute", "HashKeyAttributeValue")
.withFilterExpression("data.byUserId = :x")
.withValueMap(expressionAttributeValues);
ItemCollection<QueryOutcome> items = table.query(spec);
Iterator<Item> iterator = items.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
Some important guidelines:
一些重要的指导方针:
Make sure the variable
tableName
has the correct table name represented as astring
.Make sure to replace the string
HashKeyAttribute
with the attribute name that represents yourHash Key
.Make sure to replace the string
HashKeyAttributeValue
with the value that represents the Hash Key that you want to match.Make sure the matched record has the
data.byUserId
with the value provided in the comparison expression. In the example the value"vl49uga5ljjcoln65rcaspmg8u"
was provided.
确保变量
tableName
具有正确的表名,以string
.确保将字符串替换为
HashKeyAttribute
代表您的Hash Key
.确保将字符串替换为
HashKeyAttributeValue
代表要匹配的哈希键的值。确保匹配的记录具有
data.byUserId
比较表达式中提供的值。在示例中"vl49uga5ljjcoln65rcaspmg8u"
,提供了该值。
Here is another example having the id
attribute as the Hash Key:
这是将id
属性作为哈希键的另一个示例:
Table table = dynamoDB.getTable(tableName);
Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
expressionAttributeValues.put(":x", "vl49uga5ljjcoln65rcaspmg8u");
QuerySpec spec = new QuerySpec()
.withHashKey("id", "25g77vmummpr4mc5mb9vq36q43")
.withFilterExpression("data.byUserId = :x")
.withValueMap(expressionAttributeValues);
ItemCollection<QueryOutcome> items = table.query(spec);
Iterator<Item> iterator = items.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}