找不到所请求操作的编解码器:[timestamp< - > java.lang.Long中]

时间:2016-10-07 21:25:19

标签: scala cassandra

我在scala中编写了这个简单的程序来查询cassandra

val session = cass.session
lazy val stmt = cass.session.prepare(
   """
     |select token(id), id, date, rId, tt
     | from foo
     | where token(id) > ?
     | and token(id) <= ?;
   """.stripMargin
)

lazy val statement = stmt.bind().setLong(0, start).setLong(1, end)

def fromRow(row: Row): Foo = {
   val token = row.getLong(0)
   val id = row.getLong(1)
   val date = row.getLong(2)
   val rId = row.getLong(3)
   val tt = row.getInt(4)
   Foo(id, date, rId, tt)
}

但是此代码失败并显示错误

[error] (run-main-4) com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [timestamp <-> java.lang.Long]
com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [timestamp <-> java.lang.Long]
    at com.datastax.driver.core.CodecRegistry.notFound(CodecRegistry.java:679)
    at com.datastax.driver.core.CodecRegistry.createCodec(CodecRegistry.java:526)
    at com.datastax.driver.core.CodecRegistry.findCodec(CodecRegistry.java:506)
    at com.datastax.driver.core.CodecRegistry.access$200(CodecRegistry.java:140)
    at com.datastax.driver.core.CodecRegistry$TypeCodecCacheLoader.load(CodecRegistry.java:211)
    at com.datastax.driver.core.CodecRegistry$TypeCodecCacheLoader.load(CodecRegistry.java:208)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3542)

3 个答案:

答案 0 :(得分:3)

根据链接here,将Date类型的var绑定到您的语句:

stmt.bind(new Date(start), new Date(end))

答案 1 :(得分:0)

在表FOO中,有一个时间戳类型列,但您将插入查询中的值传递给该列。您可以将日期列类型定义为时间戳。插入时间戳类型值&#39; 2012-12-07T10:00:00-0000&#39;看起来像这样。

答案 2 :(得分:0)

对于驱动程序v4.0,timestamp被映射到java.time.Instant,因此: stmt.bind(Instant.ofEpochSecond(start), Instant.ofEpochSecond(end))