How do I convert Java collections into Scala in Cassandra Datastax driver

Manu Chadha

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.

Chaitanya Waikar

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.

edited at
0

Comments

0 comments
Login to comment

Related

How should I check if resultset is empty or null using datastax cassandra driver for java

How to use prepared statement efficiently using datastax java driver in Cassandra?

How to efficiently use Batch writes to cassandra using datastax java driver?

How Cassandra handle blocking execute statement in datastax java driver

How to add arbitrary columns to Cassandra using CQL with Datastax Java driver?

Unable to convert a ResultSet into a List in Cassandra datastax driver

datastax cassandra java driver PlainTextAuthProvider example

Datastax Cassandra java driver RetryPolicy for Statement with paging

datastax cassandra java driver issue with scala conversions (play 2.3.x)

Cassandra DataStax driver: how to page through columns

how do I find out if the update query was successful or not in Cassandra Datastax

Unable to connect to one of the Cassandra nodes using Cassandra Datastax Java Driver

How to set up Cassandra client-to-node encryption with the DataStax Java driver?

Cassandra difference between com.datastax.oss -> java-driver-core and com.datastax.cassandra -> cassandra-driver-core

Does Accessor of datastax cassandra java driver use pagination?

Datastax Cassandra java driver - Object mapper - Auto create tables

Cassandra: Selecting a Range of TimeUUIDs using the DataStax Java Driver

Datastax Java Cassandra Driver: Multiple AND statements using WHERE?

Can't retrieve by UDT key from cassandra with datastax java driver

Java Cassandra Datastax driver custom codec not handling collection types

How to create a Cassandra connection pool using datastax driver

How to delete table on Cassandra using C# Datastax driver?

PHP-Driver DataStax Cassandra DB - How to use?

Cassandra Datastax Driver - Connection Pool

Datastax Cassandra Driver throwing CodecNotFoundException

How do I convert a Java byte array into a Scala byte array?

How can I convert Iterable <com.datastax.driver.core.Row> to Dataset?

What do I map varchar type to with Cassandra Java driver 4.13.0?

How can we convert com.datastax.driver.core.LocalDate to java.util.Date?

TOP Ranking

HotTag

Archive