0

How can I append a new element to a set which is in a custom type in Cassandra.

custom_type is :

CREATE TYPE custom_type (
   normal_type    TEXT,
   set_type Set<TEXT>
);

and the table to be updated is :

CREATE TABLE test_table (
   id          TEXT,
   my_type      FROZEN<custom_type>,
   clustering_key TEXT,
   PRIMARY KEY ((id),clustering_key)
);

Tried below query but did not work.

@Query("update test_table set  my_type.set_type = my_type.set_type + {'newelement'} where id=?1 and clustering_key=?2")

Any Idea on how to do that? Using [cqlsh 5.0.1 | Cassandra 3.11.1 | CQL spec 3.4.4

cacert
  • 2,677
  • 10
  • 33
  • 56

1 Answers1

1

When you say frozen, then the whole value is treated as one piece (blob), so you can't update parts of this field. Official documentation states:

When using the frozen keyword, you cannot update parts of a user-defined type value. The entire value must be overwritten. Cassandra treats the value of a frozen, user-defined type like a blob.

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • when I try without frozen the following error occurs.""Non-frozen UDTs with nested non-frozen collections are not supported"" Apparently it does not allow non frozen collection in udts. Then the only solution is first selecting the udt and then update the whole since I don't have the udt object beforehand ? Do you have any other suggestion other than that ? – cacert Feb 07 '18 at 13:46
  • Yes, with `frozen` it's only the way to do in Cassandra. If you want to guarantee that nobody else changed the data in the meantime, you can add some field that will be used as guard, and use lightweight transactions (`UPDATE` with `IF` condition) - but this will come with performance impact as data need to be read before writing – Alex Ott Feb 07 '18 at 14:22