I am storing a scala case class data in Cassandra table, for that, I need to define User-defined type. I can write cql query but do not know how to parse it.com.datastax.driver.mapping.annotations.UDT
I have tried this annotation but it does not work me. I think I'm completely out of the track.
I have also tried Session class belong to com.datastax.driver.core.Session.
and my conclusion is I have no idea how to do it I am just using hit and trail.
case class Properties(name: String,
label: String,
description: String,
groupName: String,
fieldDataType: String,
options: Seq[OptionalData]
)
object Properties{
implicit val format: Format[Properties] = Json.format[Properties]
}
case class OptionalData(label: String, name: String)
object OptionalData{
implicit val format: Format[OptionalData] = Json.format[OptionalData]
}
and my query is:
val optionalData: String=
"""
|CREATE TYPE IF NOT EXISTS optionaldata(
|label text,
|name text
);
""".stripMargin
val createPropertiesTable: String = """
|CREATE TABLE IF NOT EXISTS prop(
|name text Primary Key,
|label text,
|description text,
|groupname text,
|fielddatatype text,
|options LIST<frozen<optionaldata>>
);
""".stripMargin
com.datastax.driver.core.exceptions.InvalidQueryException: Unknown typ e leadpropdb3.optionaldata java.util.concurrent.ExecutionException: com.datastax.driver.core.exceptions.InvalidQueryException: Unknown type leadpropdb3.optionaldata at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:552) at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:513) at akka.persistence.cassandra.package$ListenableFutureConverter$$anon$2.$anonfun$run$2(package.scala:25) at scala.util.Try$.apply(Try.scala:213) at akka.persistence.cassandra.package$ListenableFutureConverter$$anon$2.run(package.scala:25) at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Unknown type leadpropdb3.optionaldata
From error message it's clear that the type wasn't created - you need to create it before creating the table - be very careful when executing CQL statements from your code - you need to wait until schema is an agreement, before you execute next statement. Here is an example of Java code that does this - it's easy to convert it into Scala.
When you're using Object Mapper with Scala, you need to obey some rules (I hope that my blog post on that topic will be published soon):
List
instead of Seq
, etc., or use extra codecs for Scala;but otherwise it's possible to use object mapper with Scala, like this:
@UDT(name = "scala_udt")
case class UdtCaseClass(id: Integer, @(Field @field)(name = "t") text: String) {
def this() {
this(0, "")
}
}
@Table(name = "scala_test_udt")
case class TableObjectCaseClassWithUDT(@(PartitionKey @field) id: Integer,
udt: UdtCaseClass) {
def this() {
this(0, UdtCaseClass(0, ""))
}
}
// ...
val mapperForUdtCaseClass = manager.mapper(classOf[TableObjectCaseClassWithUDT])
val objectCaseClassWithUDT = mapperForUdtCaseClass.get(new Integer(1))
println("ObjWithUdt(1)='" + objectCaseClassWithUDT + "'")
More examples are available in my repo.