Thursday, June 23, 2011

Querying Secondary Index in cassandra using hector api 0.7


USING SECONDARY COLUMN INDEXES

Secondary column indexes are a new feature to Cassandra 0.7. Though similar to a RangeSlicesQuery and it’s underlying get_range_slices API call, IndexSlicesQuery requires some configuration in the ColumnFamily. The following example creates a ColumnFamily called Users with an index on the birthdate column name.
- name: Users
  default_validation_class: LongType
  column_metadata:
- name: birthdate
  validator_class: LongType
  index_type: KEYS
The following IndexedSlicesQuery uses this ColumnFamily to return a slice of all the rows containing the name and birthdate columns where birthyear has a value of 1975.
IndexedSlicesQuery<String, String, Long> indexedSlicesQuery =
HFactory.createIndexedSlicesQuery(keyspace, se, se, LongSerializer.get());
indexedSlicesQuery.addEqualsExpression("birthyear", 1975L);
indexedSlicesQuery.setColumnNames("birthdate","firstname");
indexedSlicesQuery.setColumnFamily("Users");
indexedSlicesQuery.setStartKey("");
QueryResult<OrderedRows<String, String, Long>> result =
indexedSlicesQuery.execute();
Rows returned by the query will be ordered in partitioner (token) order.
IndexedSlicesQuery can also have expressions applied on other columns in conjunction with the addEqualsExpression. There are two important points to note in using additional index expressions:
  • At least one equals expression against an indexed column must always be present via addEqualsExpression
  • The columns for additional clauses do not have have to be configured as indexed for the ColumnFamily
Taking these points into account, extending the above example to limit the query to the months of April to June (4 and 6 respectively) would be:
indexedSlicesQuery.addGteExpression("birthmonth", 4L);
indexedSlicesQuery.addLteExpression("birthmonth", 6L);
IndexedSlicesQuery also has the methods addGtExpression and addLtExpression for exclusive greater than and less than clauses.

4 comments:

  1. how to create index by using java client.

    ReplyDelete
  2. Is it possible to sort/order using IndexedSlicesQuery (my requirement is to get data filtered as well as ordered) is it possible to do this using IndexedSlicesQuery??
    indexedSlicesQuery.addGteExpression("birthmonth", 4L);
    // I also wnt to order the result by a given key

    ReplyDelete
  3. how to create an index on column name with a java module?? @sumit- were u able to do that? if so share it.

    ReplyDelete
  4. sorry for the late reply , see if this one helps ..
    http://stackoverflow.com/questions/10240382/how-to-create-secondary-index-in-cassandra-hector-api-programmatically

    ReplyDelete