Cassandra 2.0 使用datastax java驱动程序的Cassandra复合密钥

Cassandra 2.0 使用datastax java驱动程序的Cassandra复合密钥,cassandra-2.0,Cassandra 2.0,我是卡桑德拉的新手,我正在尝试为我的模型评估它。我正在用CQL3和Cassandra 2.0进行测试 假设我有以下域模型 public class CompositeSample1 { private String id; private List<ChildT1> childrenT1; private List<ChildT2> childrenT2; private String rootAttr1; //getters setters o

我是卡桑德拉的新手,我正在尝试为我的模型评估它。我正在用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
}
尝试插入以下模型实例

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()));
    }
  }
CompositeSample1 CompositeSample1=新的CompositeSample1();
复合示例1.setId(UUID.randomUUID().toString());
复合示例1.setRootAttr1(“A380”);
compositeSample1.setChildrenT1(新的ArrayList());
复合示例1.setChildrenT2(新的ArrayList());

对于(inti=1;i您可以更改主键部分,只使用一列创建复合分区键没有任何意义(在这种情况下,您只有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()));
        }

它应该适合您。

请在更改您的灵长类关键部件后重试,然后执行以下操作
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()));
        }