Search code examples
cassandracassandra-3.0cql3

Cassandra query works when querying only with one of the Complex Partition Key


I am a beginner in Cassandra and was going through the data-stax community for tutorials.

As per doc, in Cassandra when a table is created complex Partition Key, then when querying the table both the partition key has to be specified else the query will result in an error.

But for me, it works when I just query with just the first Partition key. Following is the example that worked for me.

Table Description:

CREATE TABLE killrvideo.videos5 (
    title text,
    added_year int,
    added_date timestamp,
    description text,
    user_id uuid,
    video_id timeuuid,
    PRIMARY KEY (title, added_year)
) WITH CLUSTERING ORDER BY (added_year ASC)
    AND bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';

** Query:**

cqlsh:killrvideo> select * from videos5 where title='DHANNYA';

Result:

 title   | added_year | added_date                      | description | user_id                              | video_id
---------+------------+---------------------------------+-------------+--------------------------------------+--------------------------------------
 DHANNYA |       2018 | 2018-04-23 11:38:12.811000+0000 |        Test | 50554d6e-29bb-11e5-b345-feff819cdc9f | ce0de9b1-46ea-11e8-839e-87bbc8b633f5

It should have thrown an error but it did not. I am wondering how hashing of Partition key is locating the data with just one of the keys.


Solution

  • The simple answer: You have defined title as the partition key and added_year as the clustering key. This is because the first key in the primary key is always the partition key unless you use parentheses to have multiple keys in the partition key. So in your example you should use the following to have title and added_year in your partition key:

    PRIMARY KEY ((title, added_year))
    

    This is further explained in another stackoverflow answer: https://stackoverflow.com/a/24953331/1516699