retreiving set from cassandra in spark scala gives type mismatch java.utils

35 Views Asked by At

I have a table in cassandra which is a set type and have case class created in scala as:

case class test(
  
  var status: Option[Set[String]]
)

now I am trying to retreive the data from cassandra to this case class in scala using cassandra java driver core as :

Option(row.getSet("status", classOf[String]))

Error :
 found   : Set[String] (in java.util) 
 required: Set[String] (in scala.collection.immutable) 

how can I access set type from cassandra to scala case set?

my full method to acess it is:

  def getData(id: String): Option[test] = {
    try {
      val result = session.execute(
        s"SELECT * FROM $Keyspace.table WHERE id=?",
        id
      )

      if (result.getAvailableWithoutFetching != 1) {
        logger.error(s"Failed to find $Id")
        None
      } else {
        val row = result.one()
        Some(test(
          id,
         Option(row.getSet("customer_status", classOf[String]))
        ))
      }
    } catch {
      case e: IllegalArgumentException =>
        logger.error(s"Requested column names incorrect when attempting to get customer record: $e")
        None
      case e: Exception =>
        logger.error(s"Exception in getting customer record for customer $id: $e")
        None
    }
  }
1

There are 1 best solutions below

0
Programmer On

I got it working with

Option(row.getSet("status", classOf[String]).asScala.toSet)

and had to import:

import scala.collection.JavaConverters._