java如何从DynamoDB中的非键属性中搜索一个项?
我在DynamoDB中有一个表,有分区键和排序键。例如,表结构如下所示
// Primary Keys
id -> PK
name -> SK
Info -> json Formatted Attributes
假设表中有两项
// Item 1
{
id: 10245-25-45,
name: "Name 1",
info: {
userId: 1012, // PK
userName: "ABC", // SK
type: "type 1",
date: "2020-01-03 04:05:12"
}
}
// Item 2
{
id: 10245-65-70, // pK
name: "Name 2", // SK
info: {
userId: 1013,
userName: "ABCD",
type: "type 2",
date: "2020-01-03 04:10:12"
}
}
我使用Java
作为语言,并使用DynamoDBMapper
执行CRUD操作。所以我知道我可以使用DynamoDBMapper.load()
通过提供PK
和SK
来获得一个项目,
但我想知道如何通过搜索非关键属性来获取数据。例如,我需要通过提供
// userId, userName, type, data are unique values so it will give only one record if exists
find item by userId;
find item by userId and userName;
find item by type and date;
以上是我的一些访问模式,基本上,我需要在不提供PK和SK的情况下获取数据,而且我不能使用全局二级索引。所以我知道有疑问。但我想知道,没有这些,有什么办法可以满足我的需要吗?如果有人能帮助我,我真的很感激。提前谢谢
# 1 楼答案
这在dynamoDB中是不可能做到的,因为数据是分布式的,而完整的表扫描否定了此存储选项的使用
如果数据需要可搜索,则应使用搜索数据库;如果数据量较小,则可以使用SQL类型的数据库
对于健壮的解决方案,我将使用弹性搜索(AWS有一个托管解决方案),在弹性搜索中,只填充可搜索的字段,在您的例子中是
userID
、name
、date
等,并将文档的id存储在dynamoDB中因此,搜索API将查询并检索项目id,然后从dynamoDB检索项目