Search code examples
databasecassandracql

Is there a way for Cassandra to ignore clustering key when inserting?


Right now I have a table with three columns: GroupId,ObjectId and Data, with first two defined as partition key.

Currently it works as desired: if GroupIdand ObjectId match existing row, it gets overwritten.

I'm trying to add sorting by date, so I added third column, LastModified and specified it as clustering key. While it works for sorting, now I have multiple rows sharing the same GroupIdand ObjectId pairs, which is not what I need.

How can I achieve previous behaviour?

  1. I could read table before writing and delete matching row before writing a new one.
  2. I could, after reading, filter rows in my application.

I dislike both solutions because they seem to be too complicated and performance is a big concern. Is there a better way?


Solution

  • Here is how i did this in case someone else faces same problem:

    I have a table with GroupId and ObjectId as key. I'm not sure if it matters, but ObjectId is defined as clustering key.

    Then you get desired result from following view:

            CREATE MATERIALIZED VIEW IF NOT EXISTS objectlistbylast
            AS SELECT * FROM objectlist
            WHERE groupid IS NOT NULL AND objectid IS NOT NULL AND lastmodified IS NOT NULL
            PRIMARY KEY(groupid , lastmodified, objectid )
            WITH CLUSTERING ORDER BY(lastmodified DESC);
    

    Note that ordering when defining primary key matters.