[英]Dynamodb query from multiple keys
I'm relatively new to dynamodb, i'm trying to create a social media app using was dynamodb.我对 dynamodb 比较陌生,我正在尝试使用 dynamodb 创建一个社交媒体应用程序。 I have a user followers table that looks like the below schema.
我有一个类似于以下架构的用户关注者表。
And this is how my posts table look like这就是我的帖子表的样子
I am seeking a method to retrieve all posts created by followers of a specific user from the DynamoDB database.我正在寻找一种方法来从 DynamoDB 数据库中检索特定用户的关注者创建的所有帖子。 My initial plan was to first query the followers table to obtain a list of user IDs for the followers and then use those keys to query the posts table.
我最初的计划是首先查询 followers 表以获得 followers 的用户 ID 列表,然后使用这些键查询 posts 表。 However, I have been unable to determine a way to effectively query a table with a list of keys using DynamoDB.
但是,我一直无法确定一种使用 DynamoDB 有效查询包含键列表的表的方法。 Additionally, I require the retrieved posts to be sorted by date and the querying process to support pagination through the use of LastEvaluatedKey, ExclusiveStartKey, and PageCount.
此外,我要求检索到的帖子按日期排序,并要求查询过程通过使用 LastEvaluatedKey、ExclusiveStartKey 和 PageCount 来支持分页。 Is it possible to accomplish this efficiently and with good performance using DynamoDB, or should I consider revising my data model?
是否可以使用 DynamoDB 以良好的性能高效地完成此任务,或者我是否应该考虑修改我的数据模型?
(I am using nodejs) (我正在使用 nodejs)
You have given a single access pattern:您已提供单一访问模式:
I am seeking a method to retrieve all posts created by followers of a specific user from the DynamoDB database
我正在寻找一种方法来从 DynamoDB 数据库中检索特定用户的关注者创建的所有帖子
pk ![]() |
sk![]() |
followers![]() |
---|---|---|
user1![]() |
USER#user1![]() |
[user2, user4, user99] ![]() |
user2![]() |
USER#user2![]() |
[user6, user4, user99] ![]() |
pk ![]() |
sk![]() |
post![]() |
---|---|---|
user2![]() |
COMMENT#00001#DATE![]() |
some comment![]() |
user2![]() |
COMMENT#00002#DATE![]() |
some comment![]() |
user2![]() |
COMMENT#00003#DATE![]() |
some comment![]() |
user2![]() |
REPLY#00001#DATE![]() |
some reply![]() |
user2![]() |
POST#00001#DATE![]() |
some post![]() |
user2![]() |
POST#00002#DATE![]() |
some post![]() |
user99![]() |
COMMENT#00001#DATE![]() |
some comment![]() |
user99![]() |
COMMENT#00002#DATE![]() |
some comment![]() |
user99![]() |
COMMENT#00003#DATE![]() |
some comment![]() |
user99![]() |
REPLY#00001#DATE![]() |
some reply![]() |
GetItem
for user user1
user1
的GetItem
Query
for each userId
returned from above and state that sk
begins_with POST
userId
运行一个Query
并声明sk
begins_with POST
SELECT followers FROM Users WHERE pk = 'user1' AND sk='USER#user1'
FOR user in followers:
SELECT * FROM POSTS WHERE pk = user AND sk BEGINS_WITH 'POST'
You could model both tables in a single table, but it doesn't provide much benefits other than having a single table to manage:您可以在单个表中对两个表进行建模,但除了要管理单个表之外,它并没有提供太多好处:
pk ![]() |
sk![]() |
post![]() |
followers![]() |
---|---|---|---|
user1![]() |
USER#user1![]() |
[user2, user4, user99] ![]() |
|
user2![]() |
COMMENT#00001#DATE![]() |
some comment![]() |
|
user2![]() |
COMMENT#00002#DATE![]() |
some comment![]() |
|
user2![]() |
COMMENT#00003#DATE![]() |
some comment![]() |
|
user2![]() |
REPLY#00001#DATE![]() |
some reply![]() |
I don't particularly like this format, if it were my application I would model it something similar to the DynamoDB Forum Example .我不是特别喜欢这种格式,如果它是我的应用程序,我会为它建模类似于DynamoDB 论坛示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.