Search code examples
javacassandrahector

Cassandra - Hector: Search by Row-Key but filter by further expression?


create column family Records with
          comparator = UTF8Type and
          column_metadata =
          [
            {column_name: name, validation_class: UTF8Type},
            {column_name: label, validation_class: UTF8Type},
            {column_name: releasedate, validation_class: LongType},

I want to query the records by Row-Key and then further do a check on the releasedate (a timestamp value). As far as I understand this: http://www.datastax.com/dev/blog/whats-new-cassandra-07-secondary-indexes, this should easyly be possible as I query first by row key and then narrow down by releasedate.

But how to do this in hector?

Using SliceQuery does not work. I know there is IndexedSliceQuery but I do not have s secondary index set (as can be seen abolve). Is it neverthelss correct and an must to use IndexedSliceQuery (even though I do not have a seconary Index)??

Thanks Markus


Solution

  • If you know that you need to query your data by release date then, with Apache Cassandra, you should store it that way.

    This can be done in two ways: - adjust the meta data in the column family definition above to include an index on releaseddate:

    update column family Records with column_metadata=[{column_name: releasedate, validation_class: LongType, index_name:releasedate_idx, index_type:0}];    
    
    • or create an additional column family that uses the release date as the key and stores the keys to the record column family as column names. This basically does the same thing as the secondary index definition above, but makes the process of managing updates and querying more manual.

    I would suggest the first approach, giving you access to IndexedSlicesQuery and all its goodness.