Manu Chadha
Manu Chadha

Reputation: 16723

unable to get the Row from ResultSet

The following function saves data in cassandra. It calls the abstract rowToModel method defined in the same class to convert the data returned from cassandra into the respective data model.

def saveDataToDatabase(data:M):Option[M] = { //TODOM - should this also be part of the Repository trait?
    println("inserting in table "+tablename+" with partition key  "+partitionKeyColumns +" and values "+data)
    val insertQuery = insertValues(tablename,data)
    println("insert query is "+insertQuery)
    try {
      val resultSet:ResultSet = session.execute(insertQuery) //execute can take a Statement. Insert is derived from Statement so I can use Insert.
      println("resultset after insert: " + resultSet)
      println("resultset applied: " + resultSet.wasApplied())
      println(s"columns definition ${resultSet.getColumnDefinitions}")
      if(resultSet.wasApplied()){
        println(s"saved row ${resultSet.one()}")
        val savedData = rowToModel(resultSet.one())
        Some(savedData)
      } else {
        None
      }
    }catch {
      case e:Exception => {
        println("cassandra exception "+e)
        None
      }
    }
  }

The abstract rowToModel is defined as follows

override def rowToModel(row: Row): PracticeQuestionTag = {
   PracticeQuestionTag(row.getLong("year"),row.getLong("month"),row.getLong("creation_time_hour"),
      row.getLong("creation_time_minute"),row.getUUID("question_id"),row.getString("question_description"))

  }

But the print statements I have defined in saveDataToDatabase are not printing the data. I expected that the prints will print PracticeQuestionTag but instead I see the following

I expect to see something like this - PracticeQuestionTag(2018,6,1,1,11111111-1111-1111-1111-111111111111,some description1) when I print one from ResultSet`. But what I see is

resultset after insert: ResultSet[ exhausted: false, Columns[[applied](boolean)]] resultset applied: true columns definition Columns[[applied](boolean)] saved row Row[true] row to Model called for row null cassandra exception java.lang.NullPointerException

Why ResultSet, one and columnDefinitions is not showing me the values from the data model?

Upvotes: 0

Views: 640

Answers (1)

Abhishek Garg
Abhishek Garg

Reputation: 2288

This is by design. The resultset of an insert will only contain a single row which tells if the result was applied or not.

When executing a conditional statement, the ResultSet will contain a single Row with a column named “applied” of type boolean. This tells whether the conditional statement was successful or not.

It makes sense also as ResultSet is supposed to return the result of the query and why would you want to make the result set object heavy by retuning all the inputs in the result set itself. More details can be found here.

Ofcourse Get queries will have the detailed result set.

Upvotes: 2

Related Questions