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

Bra*_*wne 1 client cassandra datastax-java-driver datastax

我的代码,适用于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)
Run Code Online (Sandbox Code Playgroud)

And*_*ert 6

我假设您必须使用2.2.0-rc2(在存在CodecRegistry的情况下)或使用2.2分支.这不是一个错误,但我同意这是一个非常误导.只是为了澄清,2.2文档中是否有任何代码表明getDate()应该有效timestamp

在java-driver 2.0和2.1中getDate() 映射到CQL timestamp类型并返回Date对象:

/**
 * 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);
Run Code Online (Sandbox Code Playgroud)

但是,在java-driver 2.2+中,它会映射到类型getDate() 映射date并返回LocalDate对象:

/**
 * 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);
Run Code Online (Sandbox Code Playgroud)

这是因为在卡桑德拉2.2 是为增加新的定制列表类型timedate.因此,在驱动程序中更新方法定义似乎是合适的.您现在可以使用将timestamp类型检索为java.util.Date对象getTimestamp().此更改记录在第14节的2.2.0-rc2 升级指南中:

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.
Run Code Online (Sandbox Code Playgroud)