The schema of my Cassandra
table is
id uuid PRIMARY KEY,
description text,
messages list<text>,
image list<blob>,
primary text,
tags set<text>,
title text,
more text
I am able to retrieve a ResultSet
from the table but I don't know how to map it into my model. The Scala
class
I want to model the row into is
case class Data (id: Option[UUID],
description: String,
messages: List[String],
image: Array[Byte],
first: String,
tags: Set[String],
title: String,
more:String)
I know that the function Row.getString("column name")
coverts text
into String
but I don't know what to do to convert the list<text>
, list<blob>
and set<text>
into scala classes
.
To extract set from resultSet and convert it to scala set, you can define a function as follows :-
def convertToScalaSet(row: Row, columnName: String): Set[String] =
{
val mayBeSet = Option(row.getSet(columnName, "String".getClass).toSet[String])
mayBeSet match {
case Some(set) if set.size > 0 => set
case _ => Set[String]() //this case will cover both the possibilities of set being empty or none value
}
}
On Similar lines you can create a function that will convert to scala list
def convertToScalaList(row: Row, columnName: String): List[String] =
{
val mayBeList = Option(row.getList(columnName, "String".getClass).toList)
mayBeList match {
case Some(list) if list.size > 0 => list
case _ => List[String]() //this case will cover both the possibilities of list being empty or none value
}
}
In your code then, you could call these methods as follows
val row = resultSet.one()
convertToScalaSet(row,"tags")
convertToScalaList(row,"messages")
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments