Search code examples
cassandracqlcql3

How to retrieve a date range from cassandra


I have a very simple table to store collection of IDs by a date rage

CREATE TABLE schedule_range (
  start_date timestamp,
  end_date timestamp,
  schedules set<text>,
  PRIMARY KEY ((start_date, end_date)));

I was hoping to be able to query it by a date range

SELECT *
FROM schedule_range
WHERE start_date >= 'xxx'
AND end_date < 'yyy'

Unfortunately it doesn't work this way. I've tried few different approaches and it always fail for a different reason.

How should I store IDs to be able to get them all by a date range?


Solution

  • In cassandra you only can use >, < operators with last field of primary key, in your case 'end_date'. For previous fields you must use equal operator. If you just considerate that schema maybe you could use other choices.

    One approximation is use Apache Spark. There is some projects that built an abstraction layer in Spark over Cassandra and let you make operations in cassandra such as joins, any filter, groups by ...

    Check this projects: