问题
I am trying to do a SELECT
in java into my Cassandra database. I am trying it with:
Statement statement = QueryBuilder.select()
.all()
.from(keySpaceName, tableName)
.where((QueryBuilder.eq("asset", categoryPos)))
.and(QueryBuilder.gte("date", "2006-06-08 00:00:00"))
.limit(10)
.allowFiltering()
.enableTracing();
The CQL query (already working) is
SELECT * FROM pair_tick.price WHERE asset = 1 and date>='2006-06-08 15:30:00' LIMIT 10;
When I am trying to execute this query, I get this error:
The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
com.datastax.driver.core.exceptions.InvalidQueryException: Expected 8 or 0 byte long for date (10)
at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:50)
at com.datastax.driver.core.DriverThrowables.propagateCause(DriverThrowables.java:37)
at com.datastax.driver.core.DefaultResultSetFuture.getUninterruptibly(DefaultResultSetFuture.java:244)
at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:55)
at com.nexow.services.HistoricService.getHistoric(HistoricService.java:86)
at com.nexow.HistoricController.getHistoric(HistoricController.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
How can avoid this error? Thank you in advance!
回答1:
I believe the issue here is the way the driver is converting your date value.
As stated in the DataStax driver 3.0 (http://docs.datastax.com/en/developer/java-driver/3.0/java-driver/reference/javaClass2Cql3Datatypes.html?scroll=cql-java-types) there is a new class to handle the Cassandra date type, it is the com.datastax.driver.core.LocalDate class, so you should use that class, as the example bellow:
// 1. convert your string date to LocalDate
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
LocalDate localDate = LocalDate.fromMillisSinceEpoch(sdf.parse("2006-06-08").getTime());
// 2. then pass it to your query
Statement statement = QueryBuilder.select()
.all()
.from(keySpaceName, tableName)
.where((QueryBuilder.eq("asset", categoryPos)))
.and(QueryBuilder.gte("date", localDate)
.limit(10)
.allowFiltering()
.enableTracing();
The driver encapsulates them in the LocalDate because it is cumbersome to work with raw date literals (specially since Java does not have unsigned integers), the LocalDate class aims to hide all that complexity behind utility methods to convert LocalDate instances to and from integers representing the number of days since the Epoch.
来源:https://stackoverflow.com/questions/35334912/select-in-cassandra-with-java-datastax-driver-by-timestamp