简体   繁体   中英

Update a String list column in Cassandra DB with Scala

I'm new on Cassandra and Scala, I'm working on a Kafka consumer (written in Scala) that has to update a field of a row on Cassandra from data it receives. And so far no problem.

In this row a field is a String list and when I do the update this field hasn't to change, so I have to assign the same String list to it self.

UPDATE keyspaceName.tableName
SET fieldToChange = newValue
WHERE id = idValue
AND fieldA = '${currentRow.getString("fieldA")}'
AND fieldB = ${currentRow.getInt("fieldB")}
...
AND fieldX =  ${currentRow.getList("fieldX", classOf[String]).toString}
...

But I receive even the exception:

com.datastax.driver.core.exceptions.SyntaxError: line 19:49 no viable alternative at input ']' (... 482                   AND fieldX =  [[listStringItem1]]...)

I currently haven't found anything that could help me through the web

The problem is that Scala's string representation of the list doesn't match to the Cassandra's representation of the list , so it generates errors.

Instead of constructing the CQL statement directly in your code, it's better to use PreparedStatement and bind variables to it:

  1. first, it will speedup the execution as Cassandra won't parse every statement separately;
  2. it will be easier to bind variables as you won't need to care about corresponding string representation

But be very careful with Scala - Java driver expects Java's lists, sets, maps, and base types, like, ints, etc.. You may look to java-driver-scala-extras package , but you'll need to compile it yourself, as it's not available on Maven Central.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM