2017-04-27 134 views
0

寫這個簡單的幻象DSL代碼com.datastax.driver.core.exceptions.CodecNotFoundException:編解碼器找不到請求的操作:[詮釋<-> java.lang.Long中]

case class FooRow(id: Long, dt: DateTime, et: Long, rid: Option[Long], d: Option[String] = None) 
class FooTable extends CassandraTable[FooTable, FooRow] { 
    object id extends LongColumn(this) with PartitionKey[Long] 
    object dt extends DateTimeColumn(this) with PartitionKey[DateTime] 
    object et extends LongColumn(this) with PartitionKey[Long] 
    object rid extends OptionalLongColumn(this) 
    object d extends OptionalStringColumn(this) 
    override def fromRow(r: Row): FooRow = { 
     FooRow(
     id(r), 
     dt(r), 
     et(r), 
     rid(r), 
     d(r) 
    ) 
    } 
} 

當我嘗試插入行。使用此代碼

def put(data: FooRow) : ResultSet = { 
     val query = insert 
     .value(_.id, data.id) 
     .value(_.dt, data.dt) 
     .value(_.rid, data.rid) 
     .value(_.d, data.d) 
     .value(_.et, data.et) 
     query.consistencyLevel_=(ConsistencyLevel.QUORUM) 
     Await.result(query.future(), awaitConfiguration.awaitTimeoutValue seconds) 
    } 

我得到一個錯誤

[info] - should be able to retrieve all history records by respondent id *** FAILED *** 
[info] com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [int <-> java.lang.Long] 
[info] at com.datastax.driver.core.CodecRegistry.notFound(CodecRegistry.java:679) 
[info] at com.datastax.driver.core.CodecRegistry.createCodec(CodecRegistry.java:526) 
[info] at com.datastax.driver.core.CodecRegistry.findCodec(CodecRegistry.java:506) 
[info] at com.datastax.driver.core.CodecRegistry.access$200(CodecRegistry.java:140) 
[info] at com.datastax.driver.core.CodecRegistry$TypeCodecCacheLoader.load(CodecRegistry.java:211) 
[info] at com.datastax.driver.core.CodecRegistry$TypeCodecCacheLoader.load(CodecRegistry.java:208) 
[info] at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3542) 
[info] at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2323) 
[info] at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2286) 
[info] at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2201) 
[info] at com.google.common.cache.LocalCache.get(LocalCache.java:3953) 
[info] at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3957) 
[info] at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4875) 
[info] at com.datastax.driver.core.CodecRegistry.lookupCodec(CodecRegistry.java:480) 
[info] at com.datastax.driver.core.CodecRegistry.codecFor(CodecRegistry.java:448) 
[info] at com.datastax.driver.core.CodecRegistry.codecFor(CodecRegistry.java:430) 
[info] at com.datastax.driver.core.AbstractGettableByIndexData.codecFor(AbstractGettableByIndexData.java:69) 
[info] at com.datastax.driver.core.AbstractGettableByIndexData.getLong(AbstractGettableByIndexData.java:152) 
[info] at com.datastax.driver.core.AbstractGettableData.getLong(AbstractGettableData.java:26) 
[info] at com.datastax.driver.core.AbstractGettableData.getLong(AbstractGettableData.java:95) 
[info] at com.websudos.phantom.builder.primitives.DefaultPrimitives$LongPrimitive$$anonfun$fromRow$7.apply(Primitive.scala:187) 
[info] at com.websudos.phantom.builder.primitives.DefaultPrimitives$LongPrimitive$$anonfun$fromRow$7.apply(Primitive.scala:187) 
[info] at com.websudos.phantom.builder.primitives.Primitive$$anonfun$nullCheck$1.apply(Primitive.scala:69) 
[info] at scala.util.Try$.apply(Try.scala:192) 
[info] at com.websudos.phantom.builder.primitives.Primitive.nullCheck(Primitive.scala:69) 
[info] at com.websudos.phantom.builder.primitives.DefaultPrimitives$LongPrimitive$.fromRow(Primitive.scala:187) 
[info] at com.websudos.phantom.column.PrimitiveColumn.optional(PrimitiveColumn.scala:52) 
[info] at com.websudos.phantom.column.Column.apply(Column.scala:42) 
[info] at com.abhi.FooTable.fromRow(FooService.scala:27) 

最令人困惑的事情是,如果你看一下上面的代碼,這裏絕對沒有任何地方詮釋。

爲每stacktrack錯誤是在該行

et(r), 
+0

它期望int爲某列,並且您傳遞Long值。檢查你傳遞的值是多少。 – SnehaT

+0

哦,買什麼它是否期待一個整數,當沒有int int ... –

回答

2

我能想到的唯一的事情是,你的卡珊德拉架構未帶幻象創建產生,並在您的數據庫類型列的不同從幻影推斷是有效的。

在幻影都longdatetime列會轉換到卡桑德拉bigint類型,所以你需要確保你的DB模式匹配。這聽起來像你的數據庫列之一是int,而不是long,所以當司機試圖解析記錄它炸了。這也意味着您沒有使用自動模式生成功能,而幻像可以直接使用。閱讀this瞭解更多詳情。

此外,在主題方面,在更新的幻像版本中,fromRowput方法都是宏派生的,所以您實際上不需要手動輸入它們。所以在幻影2.7.3我希望你的代碼看起來像這樣:

import com.outworkers.phantom.dsl._ 

case class FooRow(
    id: Long, 
    dt: DateTime, 
    et: Long, 
    rid: Option[Long], 
    d: Option[String] = None 
) 

abstract class FooTable extends CassandraTable[FooTable, FooRow] with RootConnector { 
    object id extends LongColumn(this) with PartitionKey 
    object dt extends DateTimeColumn(this) with PartitionKey 
    object et extends LongColumn(this) with PartitionKey 
    object rid extends OptionalLongColumn(this) 
    object d extends OptionalStringColumn(this) 

    def put(data: FooRow): ResultSet = { 

    Await.result(
     store(data).consistencyLevel_=(ConsistencyLevel.QUORUM).future(), 
     awaitConfiguration.awaitTimeoutValue seconds 
    ) 

    } 
} 
相關問題