proke
proke

Reputation: 41

How to bind variables inside collection literals in Cassandra 3.0 using java?

I have a java application in which I am parsing data and updating it to my Cassandra 3.0 database. I have been successful so far until it comes to my columns with user-defined types.

My attempt:

String statement_update = "UPDATE table SET "
            + "elements = elements + [{name: ? , associations: []}]"
            + " WHERE id = ? 

elements is a user defined type in my Cassandra 3.0 Database. It is composed of name (data type: text) and associations (data type: list).

PreparedStatement prepared_update = session.prepare(statement_update);

bound_update = prepared_update.bind(name, id);

variables name and id are of type String.

I have been trying to use a prepared statement to update my table with newly parsed data; however after the data is parsed I am having trouble adding more elements to my elements column. I get error: “Invalid list literal for elements: bind variables are not supported inside collection literals.” How do I work around or resolve this?

I have tried ways of changing the statement_update to “UPDATE table SET elements = elements + ? WHERE id = ?”, but I don’t know what kind of list (or even if its a list) to add to my bind statement.

What is needed to satisfy my user-defined type: elements?

Upvotes: 4

Views: 1930

Answers (1)

Alexus1024
Alexus1024

Reputation: 523

Try another update notation:

    session := getGocqlSession()
    err := session.Query(
        "UPDATE table SET mymapcolumn[?] = ? WHERE rowkey = ?",
        mapKey, mapValue, rowKey,
    ).Exec()

This worked for me.

I have done this on Go, but maybe this approach will help.

Upvotes: 0

Related Questions