How to use IndexedSlicesQuery in Hector API in Cassandra



Lets Say you have your desired value with Cassandra Column Name (or Cassandra Column Key) and you want to query on the values and not on the Column Name.  Cassandra provides us with a Class in Hector API named  IndexedSlicesQuery that helps us to query on the Column Vaue.


If we talk about  the same in SQL Language it will be something like:

Select * from tableName where columnName ="desired Value";

The Implementation  of the same in Hector API will be something like the code given below:

1
2
3
4
5
6
7
8
IndexedSlicesQuery<String, String, String> indexedSlicesQuery =
HFactory.createIndexedSlicesQuery(keyspace, StringSerializer.get(), StringSerializer.get(), StringSerializer.get());
indexedSlicesQuery.addEqualsExpression("columnName", "columnValue");
indexedSlicesQuery.setColumnNames("ColumnNameWantToRetrive");
indexedSlicesQuery.setColumnFamily("ColumnFamilyName");
QueryResult<OrderedRows<String, String, String>> result =
indexedSlicesQuery.execute();

Here you can give the columnName as many as you want to fetch the Values of those Column Names.

Again one of the important Propery of IndexedSlicesQuery  is that you can implement an And Condition with it in Hecotr API.

If we again go with the SQL, it will be something like:

Select * from tableName where columnName1="desired Value1" and columnName2="desired Value2";

The Implementation  of the same in Hector API will be something like:

Using AND in Hector API


1
2
3
4
5
6
7
8
9
IndexedSlicesQuery<String, String, String> indexedSlicesQuery =
HFactory.createIndexedSlicesQuery(keyspace, StringSerializer.get(), StringSerializer.get(), StringSerializer.get());
indexedSlicesQuery.addEqualsExpression("columnName1", "columnValue1");
indexedSlicesQuery.addEqualsExpression("columnName2", "columnValue2");
indexedSlicesQuery.setColumnNames("ColumnNameWantToRetrive");
indexedSlicesQuery.setColumnFamily("ColumnFamilyName");
QueryResult<OrderedRows<String, String, String>> result =
indexedSlicesQuery.execute();

Using OR in Hector API

* And as if now, since there is no support for OR in Cassandra or Hector API. So if you want to implement the same, you have to try it in your Business Logic.

Find Comments below or Add one

Shash said...

Hi Deepak,

Very nice article.. just a query... Can we use RangeSlicesQuery also to retrieve column values?? Thanks in advance.

Deepak Kumar said...

Hello Shash
Well yes we can do the same using RangeSliceQuery too..
I think this will answer you question in a much better way :)
IndexSliceQuery is deprecated with Hector 1.2+

Post a Comment