nosql - 在 DynamoDB 中建模实体和标签
问题描述
我有一个包含“目录”、“集合”等实体的应用程序。我想使用标记对实体之间的关系进行建模。例如,我可能有一个销售目录和一个销售集合。我会知道这两个实体是连接的,因为它们都有相同的标签:“销售”。
以下是我需要做的查询:
1)获取某种类型的所有实体,即目录
2) 通过 ID 获取实体
3) 获取所有带有特定标签的实体
4)通过某个标签查询实体列表,并检索与该实体关联的其余标签。
我想知道如何在 Dynamo DB 中建模
我首先认为我可以这样做:
PK = entityType_id (e.g. catalog_1)
SK = Tag (e.g. sales)
问题是我无法获得某种类型的所有实体。(1)
我想也许我可以这样做:
PK = entityType (e.g. catalog)
SK = id_tag (e.g. 1_sales)
我可以完成:在 SK 上使用 BeginsWith 和 EndsWith 的 1,2above 和 3 使用类型标签为 PK 但无法完成的 GSI 4。
将来我还希望能够按标签类型进行查询。我看不出在亚马逊推荐的一张表中或不使用 RDBMS 的情况下如何实现所有这些。
我真的很感激我能得到的任何意见或方向。
谢谢!
解决方案
您可以将其用作您的架构
| pk | sk | GSI1 PK |
| uid1 | metadata | | tags:["sales","purchase", "borrow"] | entityType:["catalogs"]
| uid1 |entityType#catalogs | entityType#catalogs
| uid1 | tag#sales | tag#sales
| uid1 | tag#purchase | tag#purchase
| uid1 | tag#borrow | tag#borrow
| uid2 |entityType#collection | entityType#collection
| uid2 | tag#borrow | tag#borrow
| uid2 | metadata | | tags: ["borrow"] | entityType:["collection"]
其中 PK 是 GSI1 的 SK(GSI1:全球二级索引)
这将解决用例,例如
获取所有销售实体
Select* where PK=entityType#sales in table GSI1
通过 id 获取实体
Select * wehere PK=id and SK=metadata
获取所有带有 Sales 标签的实体
Select* where PK=tag#sales in table GSI1
通过特定标签查询实体列表并检索与该实体关联的其余标签。
Use 3rd query and get all list of uids and then do a get all tags, or duplicate tags data as well in every row.
推荐阅读
- angular - Angular 7返回Observable不起作用
- azure-devops - Azure DevOps 错误 MSB4184:无法评估表达式“[MSBuild] :: NormalizePath (path)”
- unity3d - 基于 Unity 的 HoloLens 应用程序无法接收 TCP 数据
- react-native - polidea BLE解码特征值在本机反应
- php - 使用 composer 的 autoload 为 tinybutstrong 加载 OpenTBS 插件
- docker - 如何使用 VSCode 的远程开发扩展在容器外的 Docker 容器中打开文件
- swift - UICollectionView 列表中的折叠部分
- database - 如何克隆 Grakn 密钥空间
- javascript - 角度路由:路由总是重定向到 404 页面
- r - 无论操作系统如何,如何使 CRAN 包只下载一次数据?