Search code examples
cassandradatastax-java-driver

unable to store base64 image data in Cassandra using cassandra driver


My client application sends image data to my controller using base64 encoding which I suppose is a string of characters.

I parse the json into the following case class

case class PQ(id: Option[UUID],
                              d: String,
                               h: List[String],
                               image: List[String], 
                              s: String,
                               f:String,
                               t: Set[String],
                              t1: String,
                              a:String,
                               r:List[String])  

While performing the database query, I am creating the insert query as follows

def insertValues(tableName:String, model:PQ):Insert = {
    println(s"insert values are ${model}")


    QueryBuilder.insertInto(tableName).value("id",model.id.get)
      .value("a",model.a)
      .value("d",model.d)
      .value("f",model.f)
      .value("h",seqAsJavaList(model.h))
      .value("image",seqAsJavaList(model.image)) 
      .value("r",model.r)
      .value("s",model.s)
      .value("t",setAsJavaSet(model.t))
      .value("t1",model.t1)
      .ifNotExists(); 
  }

The database schema is

(
    id uuid PRIMARY KEY,
    a text,
    d text,
    f text,
    h list<text>,
    image list<text>,
    r list<text>,
    s text,
    t set<text>,
    t1 text
)

But I am unable save the data in the database. The data I am inserting is

PQ(Some(11111111-1111-1111-1111-111111111111),some d,List(h),List(some image data),s test,f test,Set(t),some t1,some a,List(r1))

I am getting the following error

insert query is INSERT INTO p_q (id,a,d,f,h,image,r,s,t,t1) VALUES (?,?,?,?,?,?,?,?,?,?) IF NOT EXISTS;
cassandra exception com.datastax.driver.core.exceptions.InvalidTypeException: Value 6 of type class scala.collection.immutable.$colon$colon does not correspond to any CQL3 type

Solution

  • I detected that something is corrupting the data because of ????? in the prints.

    The problem was not with the image but with the field r. It is defined as a List but I wasn't adding it using seqAsJavaList. This was corrupting the fields. The way I debugged the issue might be useful for others.

    I created individual Insert queries and printed them. It helped me locate which Insert was causing the corruption.

      def insertValues(tableName:String, model:PracticeQuestion):Insert = {
        println(s"insert values are ${model}")
    
    
        val r1 = QueryBuilder.insertInto(tableName).value("question_id",model.question_id.get) 
        println(s"after id${r1}")
    
        val r2 = r1.value("answer",model.answer)
        println(s"after answer ${r2}")
    
        val r3 = r2.value("description",model.description)
        println(s"after desc ${r3}")
    
        val r4 = r3.value("fail_test",model.fail_test)
        println(s"after fail ${r4}")
    
        val r5 = r4.value("hints",seqAsJavaList(model.hints)) 
        println(s"after hints ${r5}")
    
    
        val r6 = r5.value("references",seqAsJavaList(model.references))
        println(s"after references ${r6}")
    
        val r7 = r6.value("success_test",model.success_test)
        println(s"after success ${r7}")
    
        val r8 = r7.value("tags",setAsJavaSet(model.tags))
        println(s"after tags ${r8}")
    
        val r9 = r8.value("title",model.title)
        println(s"after title ${r9}")
    
        val r10 = r9.value("image",seqAsJavaList(model.image))
        println(s"after image ${r10}")
    
        r10
      }