Search code examples
cassandracql3datastax-java-driver

How do you add a single element to a CQL set type using the java driver


I am trying to add a single element to a set type using datastax java driver and a bound statement, i.e. (syntax below not tested)

CREATE TABLE claims (
  id bigint PRIMARY KEY,
  claimed set<text>
);

UPDATE claims SET claimed = claimed + :claimed WHERE id = :id

When I run my BoundStatement, I receive Codec not found for requested operation: [set<varchar> <-> java.lang.String] Do I really have to write a codec?


Solution

  • With CQL appending (+) to a set expects a set. Because of this the driver is expecting a Set<String> here instead of a String. To fix this, you would nee to wrap your String value in a set, i.e.:

    PreparedStatement prepared = session.prepare("UPDATE claims SET claimed = claimed + :claimed WHERE id = :id");
    session.execute(prepared.bind(Collections.singleton("value"), id));