如何在Cassandra中存储和检索base64编码的图像 [英] How to store and retrieve base64 encoded image in Cassandra
问题描述
我正在通过 json
消息以 base64
格式发送图像
I am sending an image in base64
format in a json
message
image:["data:image/png; base64,iVBORw ...","data:image/png; base64,idfsd ..."]
我想将此图像存储在 Cassandra
中.此 json
作为 Array [String]
-
I want to store this image in Cassandra
. This json
maps to my model as an Array[String]
-
case class ImageArray(
image: Array[String]
}
我已经读过要在cassandra中存储图像,我需要ByteBuffer.因此,我使用ByteBuffer.wrap()将数组的数组不同索引转换为ByteBuffer.
I have read that to store images in cassandra, I need ByteBuffer. So I use ByteBuffer.wrap() to convert the array different indexes of the array into ByteBuffer.
ByteBuffer.wrap(model.image(0).getBytes()) //for the moment, I am taking only 1 image
我无法弄清楚如何将 ByteBuffer
转换回 String
.我可以从 Cassandra
中检索字节,如下所示
I am unable to figure out how to conovert the ByteBuffer
back into a String
. I can retrieve the bytes from Cassandra
like follows
(row.getBytes("image"))
以上内容为我提供了一个 ByteBuffer
.如何将其转换为 String
,以后可以用来创建 Array [String]
The above gives me a ByteBuffer
. How do I convert this into a String
which I could use later to create an Array[String]
推荐答案
尽管我能够解决它,但是该方法非常缓慢,因此我放弃了将图像存储为字节并将其保存为字符串(base64)的想法.这就是我从服务器上得到的.
Though I was able to solve it, the method was very slow so I dropped the idea of storing images as bytes and kept them as string (base64) as that is what I am getting from the server.
出于原始问题,为了解决该问题,我进行了一些更改.
Coming to the original issue, to solve the problem, I made a few changes.
随着Cassandra了解列表,将数组中的数组更改为列表
Changed Array to List in the model as Cassandra understand Lists
case class ImageArray(
image: List[String]
}
通过创建自定义的 Reads
-(JsPath \"image").read [List [String]]
在 cassandra
中,用于存储图像数组的表模式为 image list< blob>,
In cassandra
, the table schema for storing the image arrays was image list<blob>,
一旦在模型中有了列表,我便将其转换为 String
的 List
到 Byte
List >使用 .value("image",seqAsJavaList((((ByteBuffer.wrap(model.image(0).getBytes())).array().toList):::((ByteBuffer.wrap(model.image(1).getBytes())).array().toList)))
Once I had the List in the model, I converted it into List
of String
into List
of Byte
using .value("image",seqAsJavaList(((ByteBuffer.wrap(model.image(0).getBytes())).array().toList):::((ByteBuffer.wrap(model.image(1).getBytes())).array().toList)))
上面,我获取了图像列表的第一个元素(一个字符串,它是索引0处图像的base64表示形式),使用 ByteBuffer.wrap
将其转换为字节,然后转换为 ByteBuffer
转换为 Array
,然后将其转换为 List
.这给了我 List [Byte]
作为第一张图片.我对image(1)重复此操作,然后使用 :::
将两个图像合并.然后,我使用 seqAsJavaList
将此 Scala
List
转换为 Java
.
Above, I took the first element of image list (a string which is base64 representation of image at index 0), convert it into byte using ByteBuffer.wrap
, then converted ByteBuffer
to Array
and then converted it to List
. This gave me List[Byte]
for 1st image. I repeat it for image(1) and then join the two images using :::
. Then I convert this Scala
List
to Java
using seqAsJavaList
.
注意:由于上述方法需要多次转换,因此这不是最佳方法.相反,我决定将图像存储为List [String]
Note: As the above method required multiple conversions, it isn't a optimal way. I instead decided to store the image as List[String]
这篇关于如何在Cassandra中存储和检索base64编码的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!