【发布时间】:2014-06-10 16:23:59
【问题描述】:
我是 Cassandra 的新手,我正在尝试为我的模型评估它。我正在使用 CQL3 和 Cassandra 2.0 进行测试
假设我有以下领域模型
public class CompositeSample1 {
private String id;
private List<ChildT1> childrenT1;
private List<ChildT2> childrenT2;
private String rootAttr1;
//getters setters omitted for brevity
}
public class ChildT1 {
private String key;
private String attr1;
private String attr2;
//getters setters omitted for brevity
}
public class ChildT2 {
private String key;
private String attrA;
private String attrB;
//getters setters omitted for brevity
}
所以我想将上述模型存储在 Cassandra 中的单行中,分区键为 CompositeSample1.id。来自 2 个关系的孩子我想将它们存储为复合列
id, rootAttr1, childT1|1 {attr1, attr2}, childT1|2 {attr1, attr2} .. childT2|1 {attrA, attrB}, childT2|2 {attrA, attrB}..
我尝试了以下方法:
使用以下代码创建架构:
session.execute(String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication " +
"= {'class':'SimpleStrategy', 'replication_factor':3};", DbContext.DATABASE_NAME));
session.execute("USE "+DbContext.DATABASE_NAME);
session.execute("CREATE TABLE IF NOT EXISTS compositeSample1 (id uuid, rootAttr1 text, childT1Key text, childT1Attr1 text, , childT1Attr2 text "+
", childT2Key text, childT2AttrA text, childT2AttrB text, PRIMARY KEY ( (id), childT1Key, childT2Key))");
尝试插入以下模型实例
CompositeSample1 compositeSample1 = new CompositeSample1();
compositeSample1.setId(UUID.randomUUID().toString());
compositeSample1.setRootAttr1("A380");
compositeSample1.setChildrenT1(new ArrayList<ChildT1>());
compositeSample1.setChildrenT2(new ArrayList<ChildT2>());
for (int i=1;i<10; i++)
{
ChildT1 child1 = new ChildT1();
child1.setKey("childT1|"+i);
child1.setAttr1("T1_1_"+i);
child1.setAttr2("T1_2_"+i);
compositeSample1.getChildrenT1().add(child1);
ChildT2 child2 = new ChildT2();
child2.setKey("childT1|"+i);
child2.setAttrA("T2A"+i);
child2.setAttrB("T2B"+i);
compositeSample1.getChildrenT2().add(child2);
}
dbContext.createCompositeSample1(compositeSample1);
where the code for createCompositeSample1 is:
public void createCompositeSample1(CompositeSample1 compositeSample1) {
if (createCompositeSample1 == null)
createCompositeSample1 = session.prepare("INSERT INTO " + DbContext.DATABASE_NAME + ".compositeSample1(id, childT1Key, childT2Key, rootAttr1) VALUES(?,?,?,?)");
if (createCompositeSample1ChildT1 == null)
createCompositeSample1ChildT1 = session.prepare("INSERT INTO " + DbContext.DATABASE_NAME + ".compositeSample1(id, childT1Key, childT2Key, childT1Attr1, childT1Attr2) VALUES(?,?,?,?,?)");
if (createCompositeSample1ChildT2 == null)
createCompositeSample1ChildT2 = session.prepare("INSERT INTO " + DbContext.DATABASE_NAME + ".compositeSample1(id, childT1Key, childT2Key, childT2AttrA, childT2AttrB) VALUES(?,?,?,?,?)");
BoundStatement boundStatementParent = new BoundStatement(createCompositeSample1);
getSession().execute(boundStatementParent.bind(UUID.fromString(compositeSample1.getId()), null, null, compositeSample1.getRootAttr1()));
BoundStatement boundStatementChildT1 = new BoundStatement(createCompositeSample1ChildT1);
for(ChildT1 childT1 : compositeSample1.getChildrenT1()){
getSession().execute(boundStatementChildT1.bind(UUID.fromString(compositeSample1.getId()), childT1.getKey(), null, childT1.getAttr1(), childT1.getAttr2()));
}
BoundStatement boundStatementChildT2 = new BoundStatement(createCompositeSample1ChildT2);
for(ChildT2 childT2 : compositeSample1.getChildrenT2()){
getSession().execute(boundStatementChildT2.bind(UUID.fromString(compositeSample1.getId()), null, childT2.getKey(), childT2.getAttrA(), childT2.getAttrB()));
}
}
但我收到以下错误
com.datastax.driver.core.exceptions.InvalidQueryException:集群关键部分 childt1key 的空值无效
我的目标是:
- 为了证明我可以插入没有任何子元素的 CompositeSample1
- 将孩子添加到正在进行的 2 个关系中的任何一个
有人能指出我做错了什么或没有正确理解吗?
谢谢
【问题讨论】:
-
请在更改您的主要关键部分后尝试执行以下操作
标签: cassandra-2.0