I'm new in cassandra and I try to add to a table a set of data. My table looks like:
CREATE TABLE myTable (id int, name varchar, mySet set<uuid>, PRIMARY KEY (id));
The problem that I met is when I do my request the type are not compatible, the string representation of a set in Java is [uuid1,uuid2,...] and the representation in cql is {'uuid1','uuid2',...}
session.execute("INSERT INTO myTable (id , name, mySet) VALUES (" + myID + ", '" + myName +"' ," + mySet + ");");
So do you know guys if there is a function or a library which will directly fix this problem. Thanks a lot.
Instead of appending the set contents to the query string (which uses the set .toString() implementation) you could instead do the following (cassandra 2.0+ required):
session.execute("INSERT INTO myTable (id, name, mySet) VALUES (?, ?, ?));", myId, myName, mySet);
The driver will then take care of injecting the Set properly for you. You could also consider using a BoundStatement or QueryBuilder to accomplish this as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With