Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

dtln812 avatar image
dtln812 asked Erick Ramirez answered

Is it possible to configure how Cassandra determines uniqueness in a CQL set collection?

Hello, hope you're having a good day. I have a user defined type:

CREATE TYPE hashtag (
    id text,
    creator_id text,
    text text,
    updated timestamp
);

Used in:

CREATE TABLE post (
    id bigint,
    user text,
    thread_id text,
    hashtags set<frozen <hashtag>>,
    post_body text,
    created timestamp,
    updated timestamp,
    PRIMARY KEY ((thread_id, user), id)
) WITH CLUSTERING ORDER BY (id DESC);

Is it possible to ensure that on executing the query below 2 times - it won't add 2 values to the set, but verify uniqueness by id? If not, obviously I should use a map. Just curious if it's possible to configure in which way Cassandra defines uniqueness of a set.

UPDATE post
  SET hashtags = hashtags + {
    {
      id: 'some_hashtag_id',
      creator_id: '78ead5b6-187b-4f6e-9945-a72ab95483f3',
      text: '#nice',
      updated: toTimestamp(now())
    }
  }
  WHERE thread_id='24283c41-b829-4a7d-91c7-cd1277e0a5f7'
  AND user='78ead5b6-187b-4f6e-9945-a72ab95483f3'
  AND id=512988294041042944;
user-defined typecollections
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered

The quick answer is no. All field values of the UDT must be identical for the CQL set elements to be considered unique.

The hashtags in your data model are not identical by definition because the updated field is set to the current timestamp so will always be different. Cheers!

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.