简体   繁体   English

来自多个键的 Dynamodb 查询

[英]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这就是我的帖子表的样子

Posts API帖子接口在此处输入图像描述

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)

How to create a schema for DynamoDB如何为 DynamoDB 创建架构

  1. Define your access patterns, list them out in order of priority定义您的访问模式,按优先顺序列出它们
  2. View other models/schemas which are similar online在线查看其他类似的模型/模式
  3. Use a tool like NoSQL Workbench to create sample models with the ability to visualize使用像NoSQL Workbench这样的工具来创建具有可视化能力的示例模型
  4. Iterate continuously, make necessary changes to the schema when you spot something that will make your schema more efficient/performant.不断迭代,当您发现可以使您的架构更高效/性能更高的内容时,对架构进行必要的更改。

Your Schema你的模式

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 数据库中检索特定用户的关注者创建的所有帖子

Users用户

pk pk sk斯克 followers追随者
user1用户1 USER#user1用户#user1 [user2, user4, user99] [用户 2、用户 4、用户 99]
user2用户2 USER#user2用户#user2 [user6, user4, user99] [用户 6、用户 4、用户 99]

Posts帖子

pk pk sk斯克 post邮政
user2用户2 COMMENT#00001#DATE评论#00001#日期 some comment一些评论
user2用户2 COMMENT#00002#DATE评论#00002#日期 some comment一些评论
user2用户2 COMMENT#00003#DATE评论#00003#日期 some comment一些评论
user2用户2 REPLY#00001#DATE回复#00001#日期 some reply一些回复
user2用户2 POST#00001#DATE邮政#00001#日期 some post一些帖子
user2用户2 POST#00002#DATE邮政#00002#日期 some post一些帖子
user99用户99 COMMENT#00001#DATE评论#00001#日期 some comment一些评论
user99用户99 COMMENT#00002#DATE评论#00002#日期 some comment一些评论
user99用户99 COMMENT#00003#DATE评论#00003#日期 some comment一些评论
user99用户99 REPLY#00001#DATE回复#00001#日期 some reply一些回复

Your access pattern您的访问模式

  • GetItem for user user1用户user1GetItem
  • Parse list of followers解析关注者列表
  • Run a Query for each userId returned from above and state that sk begins_with POST为从上面返回的每个userId运行一个Query并声明sk begins_with POST
  • You will be returned each users post in order by date您将按日期顺序返回每个用户的帖子

Pseudo

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'

Single Table Design单表设计

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 pk sk斯克 post邮政 followers追随者
user1用户1 USER#user1用户#user1 [user2, user4, user99] [用户 2、用户 4、用户 99]
user2用户2 COMMENT#00001#DATE评论#00001#日期 some comment一些评论
user2用户2 COMMENT#00002#DATE评论#00002#日期 some comment一些评论
user2用户2 COMMENT#00003#DATE评论#00003#日期 some comment一些评论
user2用户2 REPLY#00001#DATE回复#00001#日期 some reply一些回复

Summary概括

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM