I have a scala code to work with cassandra with a datastax driver. I want to use INSERT …. IF NOT EXISTS
instead SELECT and INSERT
. I can’t find the right way to get INSERTED INFORMATION
after INSERT
, the session.execute returns me only FALSE or TRUE
. Could you help me write a code which lets me get data if a row with such a key is inserted ?
Connect code: private val querry= s"INSERT INTO ${keyspace}.${table} (code,date) VALUES (?, ?) IF NOT EXISTS"
override def insertIfNotExist(code: String, date: String): Boolean = {
connector.withSessionDo { session =>
val prep = connector.prepare(session, querry)
val res: Boolean = session.execute(prep.bind(code, date) // return only boolean
).wasApplied()
}
}
insert into date_table(code, date) VALUES ('5', '2028-01-01') if not exists;
if row is in table the response in console is:
[applied]| code| date
false,5,2021-11-15
and I want to acces in Scala code to 5,2021-11-15
if not only
[applied]
true
CodePudding user response:
No, the call to session.execute
always returns ResultSet
, it's only wasApplied
returns boolean. If you want to get more details, you can change it to:
val res: Boolean = session.execute(prep.bind(code, date))
if (res.wasApplied) {
// handle applied case - in this case you get only the boolean value
} else {
// handle not-applied - in this case you can extract information
// about conflicting values
}