我有一个如下所示的cassandra键空间和表:
CREATE KEYSPACE animals WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1};
--
USE animals;
CREATE TABLE alpakka (
id int PRIMARY KEY,
name text,
animal_type text
);
我正在尝试使用datastax和Alpakka这样从此表中检索所有结果:
object CassandraService {
def selectFromCassandra()(): Future[Seq[Row]] = {
val statement: Statement = new SimpleStatement(s"SELECT * FROM animals.alpakka")
val rows: Future[immutable.Seq[Row]] = CassandraSource(statement).runWith(Sink.seq)
rows
}
当我得到结果时,我想使用Circe将它们转换为case类,就像这样:
import io.circe.{Decoder, Encoder}
import io.circe.generic.semiauto._
object Alpakka {
implicit val jsonDecoder: Decoder[Alpakka] = deriveDecoder[Alpakka]
implicit val jsonEncoder: Encoder[Alpakka] = deriveEncoder[Alpakka]
}
case class Alpakka(id:Int,name:String,animal_type:String)
然后我按照以下路线调用服务:
object Routes {
val routes: Route = path("routes"){
onSuccess(CassandraService.selectFromCassandra()) { seq =>
complete(seq.head.asInstanceOf[Alpakka].name)
}
}
但是,当我这样做时,出现以下错误:
Error during processing of request: 'com.datastax.driver.core.ArrayBackedRow cannot be cast to com.nk.map.model.Alpakka'. Completing with 500 Internal Server Error response. To change default exception handling behavior, provide a custom ExceptionHandler.
java.lang.ClassCastException: com.datastax.driver.core.ArrayBackedRow cannot be cast to com.nk.map.model.Alpakka
我不确定自己在做什么错-感谢您提供任何指导!