ADW ADW - 3 months ago 7
Scala Question

To multi insert in cql for scala

I want to insert the result of first query in second query, I need to know how to multi insert values in Cassandra Query Language.Please enlighten me.

def daomethod(seconditemtable:SecondItemTable):java.util.List[Row]={

val query=session.execute("select item from itemtable").all() //first query

val queryvalue=query.map(x=>x.getString("item")).toList.distinct //List(item1,item2,item3)


val secondquery="select * from seconditemtable where entity=? and item=? and date>=? and date<=?" //second query

val statement=session.prepare(secondquery)

val boundstatement= new Boundstatement(statement)

session.execute(boundstatement.bind(seconditemtable.entity,queryvalue,seconditemtable.date,seconditemtable.date)).all() //how to insert multiple value of firstquery in item column
}


Thanks in advance

Answer

You can try something like that

val secondquery=s"select * from seconditemtable where entity=? and item in (${queryvalue.mkString(",")}) and date>=? and date<=?"

to take advantage of cql in statement. More details on it https://docs.datastax.com/en/cql/3.3/cql/cql_reference/select_r.html

Comments