0

我的 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 之类的东西一次相等多个值,那该怎么办?

4

1 回答 1

2

这是因为您使用的是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
于 2013-09-26T12:45:28.127 回答