Sahil Paudel
Sahil Paudel

Reputation: 474

Clustering key columns must exactly match columns in CLUSTERING ORDER BY directive Cassandra

I am using below query to create table

CREATE table audit_log_keyspace.rules_engine_audit_logs 
        (
          id timeuuid, 
          facts map<text, text>, 
          result_set map<text, text>, 
          async_id uuid, 
          rule_set_version_id int, 
          inserted_at timestamp, 
          created_at timestamp,
          PRIMARY KEY (id)
        ) WITH CLUSTERING ORDER BY (id DESC);

Am I missing something in this create table command. I am getting Only clustering key columns can be defined in CLUSTERING ORDER directive

I found similar questions but didn't find the solution from that question.

Upvotes: 1

Views: 54

Answers (1)

Alex Ott
Alex Ott

Reputation: 87119

Primary key in Cassandra consists of two objects:

  1. Partition key - it's always the first component of the primary key. This key defines the placement of the data. You can have one or more columns as partition keys (in this case they need to be specified in parentheses)
  2. Clustering columns (0 or more) - they define sorting of the data inside partition

In your case, your primary key consists of one column, and this is by default is partition key, so you don't have clustering columns that you can define in the CLUSTERING ORDER BY clause. If you want to achieve "global sorting" of the data by id column - it's impossible, as the value is partition key is hashed, and distributed between servers, for example, 5 could be on one server, and 6 - on another, etc.

P.S. I recommend to grab free copy of the "Cassandra: The Definitive Guide, 3rd edition" book from the DataStax's site - it will provide you the necessary information about data modeling, querying the data, etc.

Upvotes: 1

Related Questions