简体   繁体   English

Cassandra 表中的 Blob 映射

[英]Map of Blobs in Cassandra table

Trying to create new table, where each row can have extra data called context, which is key/value map.尝试创建新表,其中每一行都可以有称为上下文的额外数据,即键/值映射。 Key is string and value can be few bytes (bytes array).键是字符串,值可以是几个字节(字节数组)。

The following definition doesn't compile, with the following error:以下定义无法编译,并出现以下错误:

Error:(55, 30) Cannot find primitive implementation for class Array
  object context extends MapColumn[String, Array[Byte]] 

Here is my code:这是我的代码:

case class UserJourny(
                       id: Long,
                       time: Int,
                       activity_type: Int,
                       context: Map[String, Array[Byte]]
               )


abstract class UserJournyModel extends Table[UserJournyModel, UserJourny] {
  override def tableName: String = "user_journy"

  object dyid extends BigIntColumn with PartitionKey {
    override lazy val name = "id"
  }

  object time extends IntColumn with ClusteringOrder with Descending {
    override lazy val name = "time"
  }

  object activity_type extends IntColumn
  object context extends MapColumn[String, Array[Byte]]
}

How should I do it right?我该怎么做才正确?

Use MapColumn[String, ByteBuffer] instead, which Cassandra natively understands.使用MapColumn[String, ByteBuffer]代替,Cassandra 本身就可以理解。 Then you need some basic fun stuff to encode a byte array to a buffer.然后你需要一些基本的有趣的东西来将字节数组编码到缓冲区。

val buf = ByteBuffer.wrap(Array[Byte](1, 2, 3, 4, 5))

You can also derive a primitive to make your life easier overall.您还可以派生出一个原语,使您的整体生活更轻松。

implicit val byteArrayPrimitive = Primitive.derive[
  Array[Byte],
  ByteBuffer
](ByteBuffer.wrap)(_.array)

Now you can successfully use MapColumn[String, Array[Byte]] , and Cassandra will store it as a buffer anyway, it will have map<text, buffer> type inside.现在您可以成功使用MapColumn[String, Array[Byte]] ,并且无论如何 Cassandra 都会将其存储为buffer ,它内部将包含map<text, buffer>类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM