如何使用 DynamoDB (NoSQL) 为学生/班级建模
我正在尝试使用 DynamoDB 和 NoSQL 来实现我的目的。
考虑到我需要建立学生与班级的关系,对学生表和班级表进行建模的最佳(对吗?)方法是什么。 我考虑到 DynamoDB 中没有可用的第二索引。
该模型需要回答以下问题:
哪些学生属于特定班级?
学生选修哪些课程?
谢谢
I'm trying to get my way with DynamoDB and NoSQL.
What is the best (right?) approach for modeling a student table and class tables with respect to the fact that I need to have a student-is-in-class relationship.
I'm taking into account that there is no second-index available in DynamoDB.
The model needs to answer the following questions:
Which students are in a specific class?
Which classes a student take?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个非常简单的建议(没有范围键)是有两个表:每个查询类型一个。这在 NoSQL 数据库中并不罕见。
在您的情况下,我们有:
Student
,其属性StudentId
作为(哈希类型)主键。每个项目可能有一个名为Attends
的属性,其值是类的 Id 列表。Class
,其属性ClassId
作为(哈希类型)主键。每个项目可能有一个名为AttendedBy
的属性,其值是学生的 Id 列表。执行查询会很简单。使用学生和班级之间的一个“出席”关系更新数据库需要两次单独的写入,每个表一次。
另一种设计将有一个带有哈希和范围主键的表
Attends
。每条记录代表一名学生参加一个班级的出勤情况。哈希属性可以是班级的 ID,范围键可以是学生的 ID。那么,有关班级和学生的补充数据将驻留在其他表中。A very simple suggestion (without range keys) would be to have two tables: One per query type. This is not unusual in NoSQL databases.
In your case we'd have:
Student
with attributeStudentId
as (hash type) primary key. Each item might then have an attribute namedAttends
, the value of which was a list of Ids on classes.Class
with attributeClassId
as (hash type) primary key. Each item might then have an attribute namedAttendedBy
, the value of which was a list of Ids on students.Performing your queries would be simple. Updating the database with one "attends"-relationship between a student and a class requires two separate writes, one to each table.
Another design would have one table
Attends
with a hash and range primary key. Each record would represent the attendance of one student to one class. The hash attribute could be the Id of the class and the range key could be the Id of the student. Supplementary data on the class and the student would reside in other tables, then.连接两个 Amazon DynamoDB 表
以下示例将两个 Hive 表映射到 Amazon DynamoDB 中存储的数据。然后它调用这两个表之间的联接。连接是在集群上计算并返回的。联接不会在 Amazon DynamoDB 中进行。此示例返回客户列表以及已下了两个以上订单的客户的购买情况。
To join two Amazon DynamoDB tables
The following example maps two Hive tables to data stored in Amazon DynamoDB. It then calls a join across those two tables. The join is computed on the cluster and returned. The join does not take place in Amazon DynamoDB. This example returns a list of customers and their purchases for customers that have placed more than two orders.