gpt4 book ai didi

java - 按时间戳在 Cassandra 中使用 Java(datastax 驱动程序)选择

转载 作者:行者123 更新时间:2023-11-30 08:40:59 26 4
gpt4 key购买 nike

我正在尝试在我的 Cassandra 数据库中使用 Java 执行 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$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)

如何避免这个错误?提前致谢!

最佳答案

我认为这里的问题是驱动程序转换日期值的方式。

如 DataStax 驱动程序 3.0 ( http://docs.datastax.com/en/developer/java-driver/3.0/java-driver/reference/javaClass2Cql3Datatypes.html?scroll=cql-java-types ) 中所述,有一个新类来处理 Cassandra 日期类型,它是 com.datastax.driver.core.LocalDate类,因此您应该使用该类,如下例所示:

 // 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 类旨在隐藏实用方法背后的所有复杂性以转换 LocalDate 实例表示自纪元以来的天数的整数。

关于java - 按时间戳在 Cassandra 中使用 Java(datastax 驱动程序)选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35334912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com