Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Representation of Set in java versus CQL

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.

like image 421
mel Avatar asked Feb 10 '23 09:02

mel


1 Answers

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.

like image 143
Andy Tolbert Avatar answered Feb 13 '23 07:02

Andy Tolbert