【问题标题】:PRIMARY KEY part colname cannot be restricted by IN relationPRIMARY KEY 部分 colname 不能受 IN 关系限制
【发布时间】:2013-10-02 10:25:59
【问题描述】:

我的CQL3表是这样的

  CREATE TABLE stringindice (
  id text,
  colname text,
  colvalue blob,
  PRIMARY KEY (id, colname, colvalue)
  ) WITH COMPACT STORAGE 

我在其中插入了一些值。现在,当我尝试做这样的事情时:

   QueryBuilder.select().all().from(keySpace, indTastringindice ble).where().and(QueryBuilder.eq("id", 'rowKey")).and(QueryBuilder.in("colname", "string1", "string2"));

本质上是

select * from stringindice where id = "rowkey" and colname IN ("string1", "string2")

我收到以下异常:

com.datastax.driver.core.exceptions.InvalidQueryException: PRIMARY KEY part colname cannot be restricted by IN relation
at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:35)
at com.datastax.driver.core.ResultSetFuture.extractCauseFromExecutionException(ResultSetFuture.java:214)
at com.datastax.driver.core.ResultSetFuture.getUninterruptibly(ResultSetFuture.java:169)
at com.datastax.driver.core.Session.execute(Session.java:110)

在CQL3的文档中是这样写的

"此外,IN 关系只允许在 分区键和完整主键的最后一列。”

所以好像不支持!!如果是,那么如果我必须使用 IN 之类的东西一次相等多个值,该怎么办?

【问题讨论】:

    标签: cassandra cql3 datastax-java-driver


    【解决方案1】:

    因为你使用的是compact storage,所以复合列是colname:colvalue(值为空)。这意味着colname 不是完整主键的最后一列。

    如果您不使用compact storage(推荐用于所有新数据模型),您有等效的架构:

    CREATE TABLE stringindice (
      id text,
      colname text,
      colvalue blob,
      PRIMARY KEY (id, colname)
    );
    

    那么您的IN 查询将起作用:

    cqlsh:ks> insert into stringindice (id, colname, colvalue) VALUES ('rowkey', 'string1', '01');
    cqlsh:ks> insert into stringindice (id, colname, colvalue) VALUES ('rowkey', 'string2', '02');
    cqlsh:ks> insert into stringindice (id, colname, colvalue) VALUES ('rowkey', 'string3', '03');
    cqlsh:ks> select * from stringindice where id = 'rowkey' and colname IN ('string1', 'string2');
    
     id     | colname | colvalue
    --------+---------+----------
     rowkey | string1 |     0x01
     rowkey | string2 |     0x02
    

    【讨论】:

    • 谢谢 Richard,但遗憾的是我们不得不使用 Compact Storage 出于其他一些原因。还有其他替代我们 IN 子句的方法吗?
    • 为什么需要紧凑型存储?此架构将 colvalue 存储在列键中,因此限制为 64 KB。非紧凑存储非常相似,只是 colvalue 是实际的列值。
    • 嗯,这是保持我们宽行节俭模式的最佳方式。您可以在此处groups.google.com/a/lists.datastax.com/forum/#!topic/… 的类似讨论中看到更多原因
    • 抱歉,我写的架构不等价,因为您的架构允许每个列名有多个列值。那是你需要的吗?除了使用IN,您还可以通过使用where id = 'rowkey' and colname = 'string1' 并为IN 列表中的每个值执行来获取colname 的所有值。
    猜你喜欢
    • 2015-01-30
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2015-01-20
    • 2016-02-06
    • 1970-01-01
    相关资源
    最近更新 更多