我有一张表,结构如下:
当我进行查询时,我希望能够对数据图进行查询过滤;但我不太确定如何设置查询。
这是我到目前为止所拥有的:
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));
但我构建过滤器的方式不正确,并且不断遇到以下错误:
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)
那么我应该使用什么比较运算符(因为 IN 用于列表类型),以及如何构建查询过滤器以便可以在 MAP 内部指定比较。
Thanks!
尝试比较地图属性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());
}
一些重要的指导方针:
确保变量tableName
正确的表名表示为string
.
确保替换字符串HashKeyAttribute
与代表您的属性名称Hash Key
.
确保替换字符串HashKeyAttributeValue
与代表您要匹配的哈希键的值。
确保匹配的记录有data.byUserId
与比较表达式中提供的值。示例中的值"vl49uga5ljjcoln65rcaspmg8u"
被提供。
这是另一个例子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());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)