使用datastax java驱动程序的Cassandra复合键

时间:2014-06-09 23:38:13

标签: cassandra-2.0

我是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。来自两个关系的孩子我想将它们存储为复合列

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的空值无效

我的目标是:

  1. 证明我可以插入没有任何孩子的CompositeSample1
  2. 将孩子添加到正在进行的两个关系中的任何一个
  3. 有人可以查明我做错了什么或者没有正确理解?

    谢谢

1 个答案:

答案 0 :(得分:1)

您可以更改主键部分,没有使用单列创建复合分区键的意义(在这种情况下,您只有id列作为复合分区键的一部分),创建表如下所述。

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))");

你可以插入一个空字符串而不是空值,如下面给出的

BoundStatement boundStatementParent = new BoundStatement(createCompositeSample1);

        session.execute(boundStatementParent.bind(UUID.fromString(compositeSample1.getId()), StringUtils.EMPTY, StringUtils.EMPTY,
                compositeSample1.getRootAttr1()));

        BoundStatement boundStatementChildT1 = new BoundStatement(createCompositeSample1ChildT1);
        for (ChildT1 childT1 : compositeSample1.getChildrenT1())
        {
            session.execute(boundStatementChildT1.bind(UUID.fromString(compositeSample1.getId()), childT1.getKey(),
                    StringUtils.EMPTY, childT1.getAttr1(), childT1.getAttr2()));
        }

        BoundStatement boundStatementChildT2 = new BoundStatement(createCompositeSample1ChildT2);
        for (ChildT2 childT2 : compositeSample1.getChildrenT2())
        {
            session.execute(boundStatementChildT2.bind(UUID.fromString(compositeSample1.getId()), StringUtils.EMPTY,
                    childT2.getKey(), childT2.getAttrA(), childT2.getAttrB()));
        }

它应该适合你。