Akavall
Akavall

Reputation: 86168

Is it possible to filter by 2nd cluster key without specifying 1st?

I am just starting to get a hand of cassandra.

And I am curious if I can do the following:

session.execute("CREATE TABLE IF NOT EXISTS mykeyspace.newtable 

(user_id int,
 item_id int,
 tstamp timestamp,
 PRIMARY KEY(user_id, item_id, tstamp))")

My Understanding:

I know that my user_id is a partition key, and I can only filter on it by =. My item_id and tstamp are cluster keys. I can filter of item_id by range (using equality symbols), and for a given item_id I can filter by range for tstamp, but if item_id is not specified, I cannot filter by tstamp at all.

My question:

Is there a way to specify my PRIMARY KEY such that I can:

1) filter by = for user_id (I already have it)

2) filter by = item_id (I already have it)

3) filter by range for tstamp (I don't have it)

If I change my PRIMARY KEY specification to:

PRIMARY KEY(user_id, tstamp, item_id)

Then I would no longer be able to filter by item_id. So if there was a way to somehow filter by item_id without specifying tstamp I would have my answer.

Is there a way to achieve what I am looking for?

Upvotes: 3

Views: 151

Answers (1)

RussS
RussS

Reputation: 16576

The answer to this is to build two tables. One with each primary key scheme you want. Duplicating data in tables with different schemas is relatively cheap since writes are so fast in Cassandra.

Upvotes: 5

Related Questions