Events are not json seralized in cassandra

I need my events to be stored as json in cassandra (So I can read them with some gui client directly from db).

I’ve followed lagom’s guide https://www.lagomframework.com/documentation/1.4.x/scala/Serialization.html (Enabling JSON Serialization), but events are still stored in something like binary or other format.

Here is what I’ve done:

Created Serializer Registry

object ProjectSerializerRegistry extends JsonSerializerRegistry {
  override def serializers: Seq[JsonSerializer[_]] = Seq(
    JsonSerializer[ProjectCreated],    
  )
}

Registered it:

abstract class ProjectsApplication(context: LagomApplicationContext)
  extends LagomApplication(context)
    with CassandraPersistenceComponents
    with LagomKafkaComponents
    with AhcWSComponents {

  ...
  // Register the JSON serializer registry
  override lazy val jsonSerializerRegistry = ProjectSerializerRegistry
}

Here is the event itself:

case class ProjectCreated(id: String, name: String, createdAt: DateTime) extends ProjectEvent

object ProjectCreated {
  implicit val format: OFormat[ProjectCreated] = Json.format[ProjectCreated]
}

After sending command to entity which causes ProjectCreated event and executing query select event from projects.messages I expected to see something like this in cassandra:

{
   "id": "prj-1",
   "name": "Project 1",
   "createdAt": "2018-05-04 01:16:00" 
}

But instead, I see something like this in event column:

0x7b226d657373616765223a224869227d

Did I miss something? Or may be it is some compressed or encoded json value?

It’s still stored as bytes. Have you tried to parse it as a String.

new String(bytes, "UTF-8")

Do you run that query in cqlsh? It would be nice if it could display the column as a string, but I don’t know if that is possible.

I think DBeaver Enterprise supports Cassandra and displays the bytes columns as plain text when possible (including akka-persistence-cassandra messages).

I’ve remember using blobastext before in Datastax DevCenter like a year ago… I’m pretty sure it worked and should work for cqlsh too. I found a quick example at https://stackoverflow.com/a/31821956/3705269

select column1, blobastext(value) from YourTable where key = xxx

Also see https://docs.datastax.com/en/cql/3.3/cql/cql_reference/blob_r.html.

2 Likes