简体   繁体   中英

Query fails with “com.datastax.driver.core.exceptions.InvalidQueryException: String didn't validate.”

I have a table created like this:

CREATE TABLE messages (
stream int,
sequence int,
timestamp bigint,
message blob,
PRIMARY KEY (stream, sequence)
) WITH gc_grace_seconds = 0;

Running the following query in CQLSH works perfectly fine:

select * from messages where stream = 1 and sequence >= 1 and sequence <= 100;

However, when I try to run the same via Java driver I get the following exception:

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)

I'm using parameterized querying 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));

What gives? Overall setup works, as I have another query working on a different table.

Thanks!

Have you tried adding ; to the end of your query:

public final String FETCH_CQL = "select stream, sequence, timestamp, message "
            + "from messages where stream = ? and sequence >= ? and sequence <= ?**;**";

Another option is to use prepared statements:

PreparedStatement FETCH_PS = session.prepare(FETCH_CQL);
BoundStatement boundStatement = FETCH_PS.bind(Integer.parseInt(stream), Integer.parseInt(fromSequence), Integer.parseInt(toSequence));
session.execute(boundStatement);

streamsequence字段定义为UUID类型

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM