Manu Chadha
Manu Chadha

Reputation: 16745

How to add a Scala List or Set in Cassandra?

My Cassandra's table schema is

    id uuid PRIMARY KEY,
    a text,
    d text,
    h list<text>,
    i  list<blob>,
    p  text,
    t set<text>,
    title text

I want to add data from the following case class

Data(Some(b7a8bc92-0191-4d76-9c11-df8301e20911),d,List(d),List(1),d,Set(d),d,d)

To add the data, I have written the following QueryBuilder code

def insertValues(tableName:String, model:Data):Insert = {
    QueryBuilder.insertInto(tableName).value("id",model.id.get) 
      .value("a",model.a)
      .value("d",model.d)
      .value("h",model.h) 
      .value("i",model.i)
      .value("p",model.p)
      .value("t",model.t)
      .value("title",model.title)
      .ifNotExists(); 
  }

But I am getting the following error:

Errorcom.datastax.driver.core.exceptions.InvalidTypeException: Value 3 of type class scala.collection.immutable.$colon$colon does not correspond to any CQL3 type

I am probably messing up between data types used in Scala and Cassandra but I don't know how to correct this. Eg. one issue I see is I don't know how to convert String to blob used by field i.

Upvotes: 0

Views: 632

Answers (1)

Manu Chadha
Manu Chadha

Reputation: 16745

I had to use functions from scala.collection.JavaConversions._. The working code snippet is

  .value("image",seqAsJavaList(model.image))
  .value("tags",setAsJavaSet(model.tags))

Upvotes: 3

Related Questions