![](/img/trans.png)
[英]Scala: How to convert a protobuf ByteString object to bytes?
[英]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客戶端,該客戶端將轉換為一個case類,然后在QueryBuilder中使用case類。 在json
,我正在發送該屬性的string
,該string
將存儲為blob
並且要在插入Cassandra時將該string
轉換為blob
。 但是我不知道該怎么做。
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
您可以做的第一件事是更改表方案。 因為Cassandra不支持數據類型Seq [List [Bytes]]。
如果那不可能,您可以嘗試以下方法。
問題在於Cassandra不支持List[Byte]
因此它的數據類型blob實際上接受十六進制值。 由於具有字節數組,需要將其設置為第4個字段的值,因此建議您使用BoundStatements,它具有方法setList(yourList)
這樣您可以為setList(yourList)
設置值。
你可以做這樣的事情。
BoundStatement bound = ps1.bind()
bound.setList(yourList[Byte])
session.execute(bound)
您可以參考以下內容:
https://docs.datastax.com/en/developer/java-driver/3.0/manual/statements/prepared/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.