DataStax Java驱动程序 - getDate不返回时间戳

时间:2015-08-02 20:08:21

标签: cassandra client datastax datastax-java-driver

我的代码,适用于2.1版本的驱动程序,在2.2-rc2上失败。

这是stacktrace:

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)

1 个答案:

答案 0 :(得分:6)

我假设您必须使用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个对象。此更改记录在Upgrade Guide下的2.2.0-rc2第14节

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.