Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

ppavlenko avatar image
ppavlenko asked Erick Ramirez commented

Why is the clustering order incorrect in exercise 4 of DS201?

Hello everyone. I am passing cassandra developer path.

In the Excercise 4 step 3-5 I need to create a table with default descending sorting order by added_date. I have created CQL and checked it with solution but query result does not show me correct ordering. The data is still ordering in some other order.

Could you please advise, am I doing something wrong or I missed something?

I have tried both course provided VM and Astra to verify result, but it was the same for me.

снимок-экрана-2021-01-13-в-221432.png

снимок-экрана-2021-01-13-в-221452.png

[EDIT] Adding describe table just in case.

cqlsh:killrvideo> DESCRIBE videos_by_tag;
CREATE TABLE killrvideo.videos_by_tag (
    tag text,
    added_date timestamp,
    video_id uuid,
    title text,
    PRIMARY KEY (tag, added_date, video_id)
) WITH CLUSTERING ORDER BY (added_date DESC, video_id ASC)
ds201
1 comment
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

[Update posted in original question]

[Post converted to comment since it is not an "answer"]

0 Likes 0 ·
Erick Ramirez avatar image
Erick Ramirez answered Erick Ramirez commented

The data in the table videos_by_tag is stored with tag as the partition key:

    PRIMARY KEY (tag, added_date, video_id)

Each partition has rows of data in it sorted in descending order based on the added_date column.

In the sample data, there are two partitions:

  1. tag = 'datastax'
  2. tag = 'cassandra'

The datastax partition contains two rows, one with an added date of 2013-10-16 (October) and the other with 2013-04-16 (April). The rows as shown in your screenshot are sorted in reverse chronological order.

Similarly, the cassandra partition contains three rows all sorted in reverse order starting with January 2014, followed by March 2013 and finally April 2012.

I think you are confusing "clustering order" to mean that the data will be ordered across all partitions. As the phrase suggests, the order only applies to the order of the rows within each partition. Cheers!

2 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Now I got it. Thank you a lot!

0 Likes 0 ·

Good to hear. Cheers!

0 Likes 0 ·
jtdelato avatar image
jtdelato answered

Your ordering looks ok to me. What order did you expect that you are not seeing?

Your SELECT * query looks like it returned your two partitions grouped by tag and then within each partition, your videos are sorted by added_date descending with newest videos at the top. That seems correct to me, but maybe I misunderstood your question.

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.