我继续探索卡桑德拉和我想要创建学生<=>课程关系,这是类似于许多对许多关系数据库管理系统。
在查询方面,我将使用以下查询;
假设我创建的是列族。一个是课程,另一个是学生。
CREATE COLUMN FAMILY student with comparator = UTF8Type AND key_validation_class=UTF8Type and column_metadata=[
{column_name:firstname,validation_class:UTF8Type}
{column_name:lastname,validation_class:UTF8Type}
{column_name:gender,validation_class:UTF8Type}];
CREATE COLUMN FAMILY course with comparator = UTF8Type AND key_validation_class=UTF8Type and column_metadata=[
{column_name:name,validation_class:UTF8Type}
{column_name:description,validation_class:UTF8Type}
{column_name:lecturer,validation_class:UTF8Type}
{column_name:assistant,validation_class:UTF8Type}];现在我该如何向前看?
我应该用courseID:studentId CompisiteKey创建第三列家族吗?如果是,我可以使用Hector只查询一个(左或右)复合键组件吗?
请帮帮忙。
更新:
按照建议,我创建了以下模式:
学生:
CREATE COLUMN FAMILY student with comparator = UTF8Type and key_validation_class=UTF8Type and default_validation_class=UTF8Type;然后我们将添加一些数据:
set student['student.1']['firstName']='Danny'
set student['student.1']['lastName']='Lesnik'
set student['student.1']['course.1']=''
set student['student.1']['course.2']='';创建专栏家庭课程:
CREATE COLUMN FAMILY course with comparator = UTF8Type and key_validation_class=UTF8Type and default_validation_class=UTF8Type;添加一些数据:
set course['course.1']['name'] ='History'
set course['course.1']['description'] ='History Course'
set course['course.1']['name'] ='Algebra'
set course['course.1']['description'] ='Algebra Course'最后是课程中的学生:
CREATE COLUMN FAMILY StudentInCourse with comparator = UTF8Type and key_validation_class=UTF8Type and default_validation_class=UTF8Type;添加数据:
set StudentInCourse['studentIncourse.1']['student.1'] ='';
set StudentInCourse['studentIncourse.2']['student.1'] =''; 发布于 2012-09-26 14:23:02
我在下面定义了一个数据模型,但是先对对象模型进行定义,然后再深入到行模型中比较容易,所以从PlayOrm的角度来看
public class Student {
@NoSqlId
private String id;
private String firstName;
private String lastName;
@ManyToMany
private List<Course> courses = new ArrayList(); //constructing avoids nullpointers
}
public class Course {
@NoSqlId
private String id;
private String name;
private String description
@ManyToOne
private Lecturer lecturer;
@ManyToMany
private CursorToMany students = new CursorToManyImpl();
}我本可以在课程中使用列表,但我担心如果有太多的学生参加多年的课程,我可能会得到OutOfMemory。现在,让我们跳到PlayOrm所做的事情,如果您愿意,也可以做一些类似的事情。
一个学生排在一起的样子是这样的
rowKey(the id in above entity) = firstName='dean',
lastName='hiller' courses.rowkey56=null, courses.78=null, courses.98=null, courses.101=null这是宽行,其中有许多列,它们的名称为“字段名”和“到实际课程的行键”。
课程行更多一些interesting....because用户认为加载所有的学生为一门课程可能导致内存不足,他使用一个光标,它一次只加载500个,当你循环它。
在这种情况下,PlayOrm将有两行支持该课程。所以,让我们把我们的用户排在上面,他当然是rowkey56,所以让我们来描述一下这个过程
rowkey56 = name='coursename', description='somedesc', lecturer='rowkey89ToLecturer'然后,在一些索引表中有另一行用于学生(这是一个非常宽的行,最多支持数百万学生)。
indexrowForrowkey56InCourse = student34.56, student39.56, student.23.56....
into the millions of students如果您想要一门课程有超过数百万的学生,那么无论您是否使用playOrm,都需要考虑分区。不过,如果需要的话,PlayOrm会为您进行分区。
注意:如果您不知道hibernate或JPA,那么当您加载上面的学生时,它会加载一个代理列表,所以如果您开始遍历这些课程,它就会返回到noSQL存储区并加载课程,这样您就不必这么做了;)。
当然,在这种情况下,它会加载一个代理讲师,直到您访问像lecturer.getName()这样的属性字段时才会进行填充。如果调用lecturer.getId(),它不需要加载讲师,因为它已经从课程行中加载了该内容。
编辑(更详细):PlayOrm有3个索引表(存储双倍、浮点等和BigDecimal)、整数(长、短等和BigInteger和布尔)和字符串索引表。当您使用CursorToMany时,它将根据键的FK类型使用其中的一个表。它还使用那些表,因为它是Scalable语言。它在CursorToMany上使用单独行的原因只是为了使客户端在读取一行时不会得到OutOfMemory,因为在某些情况下,toMany中可能有100万个FK。然后,CursorToMany从索引行中分批读取。
待会儿,迪恩
https://stackoverflow.com/questions/12591660
复制相似问题