Java中具有过滤器表达式的DynamoDb查询

编程入门 行业动态 更新时间:2024-10-09 17:29:06
本文介绍了Java中具有过滤器表达式的DynamoDb查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在AWS中有一个名为school-data的DynamoDb表。以下是使所有学校都具有学校名称的现有代码:

I have a DynamoDb table named school-data in AWS. Below is the existing code to get all the school with a school's name:

private DynamoDBQueryExpression<School> createQueryBySchoolName(String schoolName) { String matchSchoolName = "schoolName = :schoolName"; Map<String, AttributeValue> schoolNames = new HashMap<>(); schoolNames.put(":schoolName", new AttributeValue().withS(schoolName)); return new DynamoDBQueryExpression<School>() .withIndexName("schoolName-index") .withKeyConditionExpression(matchSchoolName) .withExpressionAttributeValues(schoolNames) .withConsistentRead(false); }

上面的查询工作正常。但是现在我需要获取所有具有特定学校名称和地址的学校。因此,表中的3列如下:

The above query works fine. But Now I need to fetch all schools with a particular school name and their address . So, below are the 3 columns in the table:

id schoolName details

详细信息列中的数据如下所示:

The data in details column looks like below:

{ "zone": "North", "type": "Convent", "address": { "id": "138", "street1": "123 Street", "street2": "456 Road" } }

因此,我需要提取所有名称为'ABC'并将street1命名为'123 Street'的学校。因此,我更新了以下查询,如下所示:

So, I need to fetch all the schools with name 'ABC' and address street1 as '123 Street'. So, I updated the above query as below:

private DynamoDBQueryExpression<School> createQueryBySchoolName(String schoolName) { String matchSchoolName = "schoolName = :schoolName"; Map<String, AttributeValue> schoolNames = new HashMap<>(); schoolNames.put(":schoolName", new AttributeValue().withS(schoolName)); schoolNames.put(":streetName", new AttributeValue().withS("123 Street")); return new DynamoDBQueryExpression<School>() .withIndexName("schoolName-index") .withKeyConditionExpression(matchSchoolName) .withFilterExpression("details.address.street1 = :streetName") .withExpressionAttributeValues(schoolNames) .withConsistentRead(false); }

但是,此操作不会返回任何数据。能否让我知道我在做什么错?

But, this returns no data. Can you please let me know what am I doing wrong?

推荐答案

您需要为DynamoDBQueryExpression设置HashKeyValues并添加页数限制

You need to set HashKeyValues for the DynamoDBQueryExpression and add the page limit

private DynamoDBQueryExpression<School> createQueryBySchoolName(String schoolName) { String matchSchoolName = "schoolName = :schoolName"; Map<String, AttributeValue> schoolNames = new HashMap<>(); schoolNames.put(":schoolName", new AttributeValue().withS(schoolName)); schoolNames.put(":streetName", new AttributeValue().withS("123 Street")); return new DynamoDBQueryExpression<Voucher>() .withHashKeyValues(schoolName) .withIndexName("schoolName-index") .withKeyConditionExpression(matchSchoolName) .withFilterExpression("details.address.street1 = :streetName") .withExpressionAttributeValues(schoolNames) .withConsistentRead(false) .withLimit(pPageSize);

}

更多推荐

Java中具有过滤器表达式的DynamoDb查询

本文发布于:2023-10-12 04:14:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483694.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   过滤器   Java   DynamoDb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!