Select 在 Cassandra 中,按时间戳 Java(datastax 驱动程序)
Select in Cassandra with Java (datastax driver) by timestamp
我正在尝试在 java 中对我的 Cassandra 数据库执行 SELECT
。我正在尝试:
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();
CQL 查询(已经工作)是
SELECT * FROM pair_tick.price WHERE asset = 1 and date>='2006-06-08 15:30:00' LIMIT 10;
当我尝试执行此查询时,出现此错误:
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.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)
如何避免这个错误?
提前致谢!
我认为这里的问题是驱动程序转换日期值的方式。
如 DataStax 驱动程序 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,因此您应该使用 class,如下例所示:
// 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();
驱动程序将它们封装在 LocalDate 中,因为使用原始日期文字很麻烦(特别是因为 Java 没有无符号整数),LocalDate class 旨在隐藏实用方法背后的所有复杂性,将 LocalDate 实例与代表自大纪元以来的天数的整数进行转换。
我正在尝试在 java 中对我的 Cassandra 数据库执行 SELECT
。我正在尝试:
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();
CQL 查询(已经工作)是
SELECT * FROM pair_tick.price WHERE asset = 1 and date>='2006-06-08 15:30:00' LIMIT 10;
当我尝试执行此查询时,出现此错误:
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.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)
如何避免这个错误? 提前致谢!
我认为这里的问题是驱动程序转换日期值的方式。
如 DataStax 驱动程序 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,因此您应该使用 class,如下例所示:
// 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();
驱动程序将它们封装在 LocalDate 中,因为使用原始日期文字很麻烦(特别是因为 Java 没有无符号整数),LocalDate class 旨在隐藏实用方法背后的所有复杂性,将 LocalDate 实例与代表自大纪元以来的天数的整数进行转换。