2

This is all occurring within a Play 2.3.x app w/ scala template

I am using this import:

import com.datastax.driver.core.Row

Along with this case class

case class timeOnPage(ip: String, pages: Map[String, Long])

I am using the following code to generate instances of timeOnPage with cassandra rows:

 private def times(row: Row): timeOnPage =
    timeOnPage(row.getString("ip"), row.getMap("page", classOf[String], classOf[Long]).toMap)

The code compiles fine, but when it is run, this error is returned:

[InvalidTypeException: Column page is a map of class java.lang.String->class java.lang.Long (CQL type map<varchar, bigint>), cannot be retrieve as a map of class java.lang.String->long]

I've tried a few different ways of declaring classOf[Long] like:

classOf[java.lang.Long]
Class.forName("java.lang.Long")

Neither type checks.

Any insight on this? Thank you in advance

plamb
  • 5,636
  • 1
  • 18
  • 31

1 Answers1

4

It's not pretty, but this will coerce your Map[String,java.lang.Long] into Map[String,Long] allowing the java-driver to properly create a Map of it's expected type and then coerce it using asInstanceOf into Map[String,Long]:

private def times(row: Row): timeOnPage =
  timeOnPage(row.getString("ip"), row.getMap("page", classOf[String], classOf[java.lang.Long]).toMap.asInstanceOf[Map[String,Long]])

This comment provides some more guidance like creating an implicit conversion for converting from Map[String, java.lang.Long] to Map[String, Long] such as:

import scala.collection.JavaConverters._

implicit def convMap(in: java.util.Map[String, java.lang.Long]): Map[String, Long] =
  in.asScala.toMap.mapValues(Long2long)

private def times(row: Row): timeOnPage =
  timeOnPage(row.getString("ip"), row.getMap("page", classOf[String], classOf[java.lang.Long]))
Community
  • 1
  • 1
Andy Tolbert
  • 11,418
  • 1
  • 30
  • 45