Reputation: 745
I am running a cassandra query, actually previously done this. but now i can't execute the query, it throws error:
Cassandra error: InvalidRequest: Error from server: code=2200 [Invalid query] message="Clustering key columns must exactly match columns in CLUSTERING ORDER BY directive"
My query is:
CREATE TABLE statistics(country_name text, dt date, confirmed_cases bigint, deaths bigint,
PRIMARY KEY(deaths))with clustering order by (deaths DESC);
Please Help!
Upvotes: 1
Views: 1194
Reputation: 1888
A primary key in Cassandra consists of one or more partition keys and zero or more clustering key components. The order of these components always puts the partition key first and then the clustering key.
deaths column in this case is a partition key and not a clustering key
For example in below query structure name1 is a partition key and name2 is the clustering key.
CREATE TABLE IF NOT EXISTS
table(column name1 data type,
column name2 data type,
column name3 data type,
PRIMARY KEY(name1,name2))
with clustering order by (name2 DESC);
Find more information here Cassandra keys
Upvotes: 1
Reputation: 57808
This is happening because you have only specified a single PRIMARY KEY. Single PRIMARY KEYs default to become partition keys. So two problems here:
There are a couple of options here. But as you want to order by deaths
, you probably should specify a different column as your partition key. Maybe partition by country_name
?
...
PRIMARY KEY (country_name,deaths))
WITH CLUSTERING ORDER BY (deaths DESC);
The caveat, is then you would need to also/always filter by country_name
in your WHERE
clause.
Upvotes: 1