2016-09-26 109 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