我有一个像这样创建的表:
CREATE TABLE messages (
stream int,
sequence int,
timestamp bigint,
message blob,
PRIMARY KEY (stream, sequence)
) WITH gc_grace_seconds = 0;
在CQLSH中运行以下查询非常正常:
select * from messages where stream = 1 and sequence >= 1 and sequence <= 100;
但是,当我尝试通过Java驱动程序运行相同的操作时,我得到以下异常:
com.datastax.driver.core.exceptions.InvalidQueryException: String didn't validate.
at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:35)
at com.datastax.driver.core.ResultSetFuture.extractCauseFromExecutionException(ResultSetFuture.java:271)
at com.datastax.driver.core.ResultSetFuture.getUninterruptibly(ResultSetFuture.java:187)
at com.datastax.driver.core.Session.execute(Session.java:126)
at com.datastax.driver.core.Session.execute(Session.java:100)
我使用参数化查询API:
public final String FETCH_CQL = "select stream, sequence, timestamp, message "
+ "from messages where stream = ? and sequence >= ? and sequence <= ?";
session.execute(FETCH_CQL, Integer.parseInt(stream), Integer.parseInt(fromSequence), Integer.parseInt(toSequence));
是什么给出的?总体设置有效,因为我有另一个查询在不同的表上工作。
谢谢!
答案 0 :(得分:0)
您是否尝试过添加;到查询结尾:
public final String FETCH_CQL = "select stream, sequence, timestamp, message "
+ "from messages where stream = ? and sequence >= ? and sequence <= ?**;**";
另一种选择是使用准备好的陈述:
PreparedStatement FETCH_PS = session.prepare(FETCH_CQL);
BoundStatement boundStatement = FETCH_PS.bind(Integer.parseInt(stream), Integer.parseInt(fromSequence), Integer.parseInt(toSequence));
session.execute(boundStatement);
答案 1 :(得分:0)
将stream
和sequence
字段定义为UUID