为 scala 嵌套案例 class 创建 cassandra table

create cassandra table for scala nested case class

我正在 Cassandra table 中存储一个 scala 案例 class 数据,为此,我需要定义用户定义类型。我可以写 cql 查询但不知道如何解析 it.com.datastax.driver.mapping.annotations.UDT 我已经尝试过此注释,但它对我不起作用。我想我完全脱离了轨道。 我也试过 Session class 属于 com.datastax.driver.core.Session. 我的结论是我不知道该怎么做我只是使用命中和跟踪。

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]
}

我的查询是:

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.$anonfun$run(package.scala:25) at scala.util.Try$.apply(Try.scala:213) at akka.persistence.cassandra.package$ListenableFutureConverter$$anon.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

从错误消息中可以清楚地看出未创建类型 - 您需要在创建 table 之前创建它 - 从您的代码执行 CQL 语句时要非常小心 - 您需要等到架构一个协议,在你执行下一个语句之前。这是执行此操作的 example of Java code - 很容易将其转换为 Scala。

当你在 Scala 中使用 Object Mapper 时,你需要遵守一些规则(我希望我关于该主题的博客 post 能尽快发布):

  1. 您需要使用 Java 类型 - List 而不是 Seq 等,或者使用 extra codecs for Scala;
  2. 案例 类 应该有空构造函数。

但除此之外,可以在 Scala 中使用对象映射器,如下所示:

@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 + "'")

my repo 中提供了更多示例。