1

I am trying to insert some values in Cassandra using Datastax Java driver.

I have a case class representing the values I want to insert.

case class User(firstname:String, lastname:String) 

The insert method of Cassandra's driver looks like

values(List<String> names, List<Object> values) 

The table schema is

CREATE TABLE users ( firstname text, lastname text, PRIMARY KEY ((firstname, lastname)) 

What is the way to convert User into List<Object>

0

2 Answers 2

2

One way to get the java.util.List<Object> without reflection:

User("a","a").productIterator.toList.map(_.asInstanceOf[Object]).asJava 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I understood the conversion except the asJava part. Where is asJava method defined. I suppose map will create a List[Object] but I didn't find asJava in List class
I tried your solution and got this error error: value asJava is not a member of List[Object]
got it working. Had to import import collection.JavaConverters._
1

Getting Map for case class prop:value with reflection, partitioning keys and values, converting to java lists.

val usr = User("John", "Doe") val map = (Map[String, AnyRef]() /: usr.getClass.getDeclaredFields) { (acc, field) => field.setAccessible(true) acc + (field.getName -> field.get(usr)) } val keys = map.keys.init.toList val vals = map.values.init.toList import collection.JavaConverters._ new SomeClass().values(keys.asJava, vals.asJava) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.