【问题标题】:DataStax Java Driver -- getDate does not return a timestampDataStax Java 驱动程序——getDate 不返回时间戳
【发布时间】:2015-10-24 21:35:28
【问题描述】:

我的代码在 2.1 版本的驱动程序上运行,但在 2.2-rc2 上运行失败。

这是堆栈跟踪:

Exception occurred in target VM: Value accountExpiryDate is of type timestamp 
com.datastax.driver.core.exceptions.InvalidTypeException: Value accountExpiryDate is of type timestamp
    at com.datastax.driver.core.AbstractGettableByIndexData.checkType(AbstractGettableByIndexData.java:75)
    at com.datastax.driver.core.AbstractGettableByIndexData.getDate(AbstractGettableByIndexData.java:192)
    at com.datastax.driver.core.AbstractGettableData.getDate(AbstractGettableData.java:26)
    at com.datastax.driver.core.AbstractGettableData.getDate(AbstractGettableData.java:113)

【问题讨论】:

  • 欢迎来到 Stackoverflow。对不起,我错过了这个问题。
  • 你能创建一个显示差异的补丁吗?这将帮助我了解发生了什么变化。
  • 欢迎您!人们对你的问题投了反对票,因为这似乎不是一个问题。 (这是一个很大的报告,对吗?)此外,鉴于 Cassandra 是开源的,正在积极开发中,在这里发布此代码而不是在 Apache 上记录错误或查找开发人员使用的电子邮件列表并询问他们似乎很奇怪它。

标签: cassandra client datastax datastax-java-driver


【解决方案1】:

我假设您必须使用 2.2.0-rc2(鉴于 CodecRegistry 的存在)或使用 2.2 分支。这不是错误,但我同意它具有误导性。澄清一下,2.2 文档中是否有任何代码表明 getDate() 应该适用于 timestamp

在 java-driver 2.0 和 2.1 getDate() maps to the CQL timestamp type and returns a Date object:

/**
 * Returns the {@code i}th value as a date.
 *
 * @param i the index ({@code 0 <= i < size()}) to retrieve.
 * @return the value of the {@code i}th element as a data. If the
 * value is NULL, {@code null} is returned.
 *
 * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
 * @throws InvalidTypeException if value {@code i} is not of type TIMESTAMP.
 */
public Date getDate(int i);

但是,在 java-driver 2.2+ 中,它改为映射到 getDate() maps to the date type and returns a LocalDate object

/**
 * Returns the {@code i}th value as a date (without time).
 *
 * @param i the index ({@code 0 <= i < size()}) to retrieve.
 * @return the value of the {@code i}th element as an date. If the
 * value is NULL, {@code null} is returned.
 *
 * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
 * @throws InvalidTypeException if value {@code i} is not of type DATE.
 */
public LocalDate getDate(int i);

这是因为在 cassandra 2.2 new CQL types were added for time and date。因此,在驱动程序中更新方法定义似乎是合适的。您现在可以使用getTimestamp()timestamp 类型检索为java.util.Date 对象。此更改记录在第 #14 节下的 2.2.0-rc2 的 Upgrade Guide 中:

Getters and setters have been added to “data-container” classes for new CQL types:

getByte/setByte for the TINYINT type
getShort/setShort for the SMALLINT type
getTime/setTime for the TIME type
getDate/setDate for the DATE type

The methods for the TIMESTAMP CQL type have been renamed to getTimestamp and setTimestamp.

This affects Row, BoundStatement, TupleValue and UDTValue.

【讨论】:

    猜你喜欢
    • 2020-11-14
    • 2016-05-30
    • 2016-05-21
    • 2013-09-06
    • 2016-11-29
    • 2016-11-29
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    相关资源
    最近更新 更多