[英]create cassandra table for scala nested case class
我在 Cassandra 表中存儲了一個 Scala 案例類數據,為此,我需要定義用戶定義的類型。 我可以編寫 cql 查詢,但不知道如何解析 it.com.datastax.driver.mapping.annotations.UDT 我已經嘗試過這個注釋,但它對我不起作用。 我想我完全脫離了軌道。 我也試過屬於com.datastax.driver.core.Session.
Session 類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:未知類型 Leadpropdb3.optionaldata java.util.concurrent.ExecutionException:com.datastax.driver.core.exceptions.InvalidQueryException:com.google.common 上的未知類型 Leadpropdb3.optionaldata。 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) ) 在 akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java: 624) 在 java.lang.Thread.run(Thread.java:748) 引起:com.datastax.driver.core.exceptions.InvalidQueryException:未知類型 Leadpropdb3.optionaldata
從錯誤消息中可以清楚地看出類型未創建 - 您需要在創建表之前創建它 - 從代碼中執行 CQL 語句時要非常小心 - 在執行下一條語句之前,您需要等到模式達成一致。 這是執行此操作的 Java 代碼示例- 很容易將其轉換為 Scala。
當你在 Scala 中使用 Object Mapper 時,你需要遵守一些規則(我希望我關於這個主題的博文能盡快發布):
但除此之外,可以在 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 + "'")
我的 repo中提供了更多示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.