如何将 Scala 字节列表转换为 blob
how to convert Scala List of bytes to blob
我的架构使用 blob
类型
id uuid PRIMARY KEY,
a text,
d text,
h list<text>,
i list<blob>,
p text,
t set<text>,
title text
我发送一个 json
fromm 客户端,它被转换成一个案例 class,然后我在 QueryBuilder 中使用案例 class。在 json
中,我正在为 属性 发送 string
,它将存储为 blob
,我想将 string
转换为 blob
插入 Cassandra 时。但是我不知道该怎么做。
json
是 PracticeQuestion(Some(2b01be60-6210-4449-ad40-6b97297d69f2),d,List(d),List(List(1)),d,Set(d),d,d)
case class
是
case class Data (id: Option[UUID],
d : String, //text in cassandra
h : List[String], //list <text>
i : List[Seq[Byte]], //list<blob>.
p : String,//string
t : Set[String], //set<text>
title : String, // text
a:String ) //text
QueryBuilder
代码是
def insertValues(tableName:String, model:PracticeQuestion):Insert = {
QueryBuilder.insertInto(tableName).value("id",model.id.get)
.value("a",model.a)
.value("d",model.d)
.value("h",seqAsJavaList(model.h))
.value("image",seqAsJavaList(model.image)) //this probably isn't correct.
.value("p",model.p)
.value("t",setAsJavaSet(model.t))
.value("title",model.title)
.ifNotExists(); }
当我 运行 我的代码时,我得到以下错误 Errorcom.datastax.driver.core.exceptions.InvalidTypeException: Value 4 of type class scala.collection.convert.Wrappers$SeqWrapper does not correspond to any CQL3 type
您可以做的第一件事是更改 table 方案。因为 Cassandra 不支持数据类型 Seq[List[Bytes]]。
如果不行,您可以尝试以下方法。
问题是 Cassandra 不支持 List[Byte]
所以它的数据类型 blob 实际上接受十六进制值。由于您拥有字节数组并且需要将其设置为第 4 个字段的值,因此我建议您使用具有方法 setList(yourList)
的 BoundStatements,这样您就可以为 preparedStatement 设置值。
你可以这样做。
BoundStatement bound = ps1.bind()
bound.setList(yourList[Byte])
session.execute(bound)
你可以参考这个:
https://docs.datastax.com/en/developer/java-driver/3.0/manual/statements/prepared/
我的架构使用 blob
类型
id uuid PRIMARY KEY,
a text,
d text,
h list<text>,
i list<blob>,
p text,
t set<text>,
title text
我发送一个 json
fromm 客户端,它被转换成一个案例 class,然后我在 QueryBuilder 中使用案例 class。在 json
中,我正在为 属性 发送 string
,它将存储为 blob
,我想将 string
转换为 blob
插入 Cassandra 时。但是我不知道该怎么做。
json
是 PracticeQuestion(Some(2b01be60-6210-4449-ad40-6b97297d69f2),d,List(d),List(List(1)),d,Set(d),d,d)
case class
是
case class Data (id: Option[UUID],
d : String, //text in cassandra
h : List[String], //list <text>
i : List[Seq[Byte]], //list<blob>.
p : String,//string
t : Set[String], //set<text>
title : String, // text
a:String ) //text
QueryBuilder
代码是
def insertValues(tableName:String, model:PracticeQuestion):Insert = {
QueryBuilder.insertInto(tableName).value("id",model.id.get)
.value("a",model.a)
.value("d",model.d)
.value("h",seqAsJavaList(model.h))
.value("image",seqAsJavaList(model.image)) //this probably isn't correct.
.value("p",model.p)
.value("t",setAsJavaSet(model.t))
.value("title",model.title)
.ifNotExists(); }
当我 运行 我的代码时,我得到以下错误 Errorcom.datastax.driver.core.exceptions.InvalidTypeException: Value 4 of type class scala.collection.convert.Wrappers$SeqWrapper does not correspond to any CQL3 type
您可以做的第一件事是更改 table 方案。因为 Cassandra 不支持数据类型 Seq[List[Bytes]]。
如果不行,您可以尝试以下方法。
问题是 Cassandra 不支持 List[Byte]
所以它的数据类型 blob 实际上接受十六进制值。由于您拥有字节数组并且需要将其设置为第 4 个字段的值,因此我建议您使用具有方法 setList(yourList)
的 BoundStatements,这样您就可以为 preparedStatement 设置值。
你可以这样做。
BoundStatement bound = ps1.bind()
bound.setList(yourList[Byte])
session.execute(bound)
你可以参考这个:
https://docs.datastax.com/en/developer/java-driver/3.0/manual/statements/prepared/