2016-09-26 108 views
1

我试图用基于时间范围的查询查询Cassandra,并且在从我的Java服务运行查询时遇到InvalidQueryException。Java CQL驱动程序查询时间戳时出现无效查询异常

比方说,我有以下模式:

CREATE TABLE user (
    user_id uuid, 
    ts timestamp, 
    data text, 
    PRIMARY KEY (user_id, ts) 
) 

使用cqlsh我能够查询,如下所示:

SELECT * FROM user WHERE user_id = c37d661d-7e61-49ea-96a5-68c34e83db3a AND Time >= '2016-09-26T16:39:15+0000'; 

在我的Java服务,下面的代码引发InvalidQueryException

Statement select = QueryBuilder.select().all() 
        .from(keyspace, tableName) 
        .where(eq("user_id", userUUID)) 
        .and(gte("ts", startTime)); 

    session.execute(select); 

抛出的错误是:

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.datastax.driver.core.exceptions.InvalidQueryException: Expected 8 or 0 byte long for date (24)] with root cause 
com.datastax.driver.core.exceptions.InvalidQueryException: Expected 8 or 0 byte long for date (24) 

如果我尝试执行查询为String,而不是一个Statement,它工作正常。这是一个例子。

String query = "SELECT * FROM user WHERE user_id = c37d661d-7e61-49ea-96a5-68c34e83db3a AND ts >= '2016-09-26T16:39:15+0000';"; 

几个问题:

  1. 为什么我会得到一个异常说:“预计8或0字节长日期”? CQL documentation表示我们可以输入时间戳integerString
  2. 我需要做什么才能使用Statement类来查询我的查询?我的服务收到格式为Date ISO 8601的时间戳。
+0

你好,你有没有找到如何解决这一问题?我最近遇到了同样的问题,这非常令人沮丧。同样的查询在cqlsh中运行,但不会从程序运行。如果我复制查询表单调试日志并粘贴在cqlsh,它运行良好... – Raj

回答

0

我能够通过在执行之前将其转换为字符串来实现此功能。

Results = cassandraSession.execute(cqlStatement.toString()); 

只有这样才能让它与时间戳一起工作。

这将工作,如果你只需要使用日期在where子句中......

Select in Cassandra with Java (datastax driver) by timestamp