如何在 DynamoDB GSI 中查找空数组值
我已经实现了全局二级索引,以使用给定参数从 DynamoDB 检索所有记录。根据实现,我能够获取与 INSTRUCTOR 角色相关的所有记录。但也有一些记录没有任何作用。然后我需要获取所有记录,包括 INSTRUCTOR 角色和空角色详细信息。
如何使用 Java 实现此场景以及如何定义以考虑过滤器表达式上的空数组值?我的代码如下。
QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("lifeCycle = :life_cycle and startDate < :s_date")
.withFilterExpression("contains (#user_roles, :roles)")
.withNameMap(expressionAttributeNames)
.withValueMap(new ValueMap()
.with(":life_cycle", "PUBLISHED")
.withString(":roles", "INSTRUCTOR")
.with(":s_date", startDate)
);
ItemCollection<QueryOutcome> items = index.query(spec);
根据上述实现,我仅获得与讲师相关的记录。但响应也应包含空角色值。
记录已按以下结构保存在我的表中,角色可以保存为“roles”:[]。那么两条记录都应该在结果中。
{
"endDate": "2022-04-21T00:00:00.000Z",
"roles": [
"INSTRUCTOR",
"STUDENT"
],
"createdDate": "2021-09-27T06:46:41.284Z",
"modifiedDate": "2021-09-27T06:46:41.284Z",
"id": "1",
"lifeCycle": "PUBLISHED",
"startDate": "2021-09-27T00:00:00.000Z"
},
{
"endDate": "2022-04-21T00:00:00.000Z",
"roles": [],
"createdDate": "2021-09-27T06:46:41.284Z",
"modifiedDate": "2021-09-27T06:46:41.284Z",
"id": "2",
"lifeCycle": "PUBLISHED",
"startDate": "2021-09-27T00:00:00.000Z"
}
I have implemented a Global Secondary Index to retrieve all the records from DynamoDB using the given parameters. According to the implementation, I was able to get all the records that are related to the INSTRUCTOR role. But there are some records without having any role. Then I need to get all the records including the INSTRUCTOR role and empty role details.
How can I implement this scenario using Java and how to define to consider the empty array value on filter expression? My code is as follows.
QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("lifeCycle = :life_cycle and startDate < :s_date")
.withFilterExpression("contains (#user_roles, :roles)")
.withNameMap(expressionAttributeNames)
.withValueMap(new ValueMap()
.with(":life_cycle", "PUBLISHED")
.withString(":roles", "INSTRUCTOR")
.with(":s_date", startDate)
);
ItemCollection<QueryOutcome> items = index.query(spec);
According to the above implementation, I'm getting only instructor related records only. but the response should include the empty role values as well.
The records have been saved in my table as the following structure and roles can be saved as "roles": []. Then both records should be in the result.
{
"endDate": "2022-04-21T00:00:00.000Z",
"roles": [
"INSTRUCTOR",
"STUDENT"
],
"createdDate": "2021-09-27T06:46:41.284Z",
"modifiedDate": "2021-09-27T06:46:41.284Z",
"id": "1",
"lifeCycle": "PUBLISHED",
"startDate": "2021-09-27T00:00:00.000Z"
},
{
"endDate": "2022-04-21T00:00:00.000Z",
"roles": [],
"createdDate": "2021-09-27T06:46:41.284Z",
"modifiedDate": "2021-09-27T06:46:41.284Z",
"id": "2",
"lifeCycle": "PUBLISHED",
"startDate": "2021-09-27T00:00:00.000Z"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够根据我在问题中提到的条件获取所有记录。为此,我在 PartiQL 中使用了 size 选项,并认为角色为空,这意味着属性的大小为 0,并且角色也可以具有特定值。
I was able to get all the records as per the conditions I have mentioned in the question. For that, I have used size option in PartiQL and considered roles are empty which means the attribute's size is 0 and roles can have particular value as well.