简体   繁体   中英

Codec not found for requested operation: [timestamp <-> java.lang.Long]

I wrote this simple program in scala to query 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)
}

However this code fails with error

[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)

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

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

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

In your Table FOO , there is a timestamp Type Column but you pass Long Value at your insertion query to that Column. May Be You Define date column type as timestamp . Insert a timestamp type value '2012-12-07T10:00:00-0000' look like this.

Codec not found for requested operation: [timestamp <-> org.joda.time.DateTime]

I was getting above error, so i had to convert Date format instead of other formats. So you need to convert your value desired type

DateTime.now().toDate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM