【问题标题】:Codec not found for requested operation: [timestamp <-> java.lang.Long]未找到请求操作的编解码器:[timestamp <-> java.lang.Long]
【发布时间】:2017-02-16 23:18:40
【问题描述】:

我在 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)

【问题讨论】:

    标签: scala cassandra


    【解决方案1】:

    在您的 Table FOO 中,有一个 timestamp 类型列,但您将插入查询中的 Long 值传递给该列。可能是您将 date 列类型定义为 timestamp。插入 timestamp 类型值“2012-12-07T10:00:00-0000”,如下所示。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 是的,我知道,但是没有关于版本的信息,现在有人可能会来这里寻找他的问题的答案(就像我今天早上一样)。
        • 感谢库巴文塔!好主意!
        【解决方案4】:
        Codec not found for requested operation: [timestamp <-> org.joda.time.DateTime]
        

        我遇到了上述错误,所以我必须转换日期格式而不是其他格式。所以你需要转换你想要的值类型

        DateTime.now().toDate
        

        【讨论】:

          猜你喜欢
          • 2017-09-25
          • 2017-04-07
          • 2017-03-24
          • 2019-05-27
          • 2018-06-08
          • 2018-01-06
          • 2019-09-30
          • 2017-05-19
          • 2021-01-29
          相关资源
          最近更新 更多