Tuesday, June 28, 2011

Cassandra Hector Api - Querying by timestamp

You can do this with SliceQuery provided by the hector api

Lets take up a example where you want the results to be fetched based on time of posting .

Then here are the simple steps you need to take .

Create the column family with type Long . we can use the System.currentTimeInMillis() as the column name .

create column family MyCF with column_type = 'Standard' and comparator = 'LongType';

You can use the SliceQuery to get the records within a particular time rang , or most recent , oldest sorted ascending or descending withing range and you can also limit the number of returned results .

Here is the code to do the querying

Lets try out the example with a SuperColumn as a Long

SuperSliceQuery<String, Long, String, String> query = HFactory.createSuperSliceQuery(keyspace, ss, 
LongSerializer.get(), ss, ss);

Here the SuperColumnSerializer is LongSerializer.get()
similarly for a Column as Long , you need to make the Column Name Serializer as LoginSerializer.get()


query.setColumnFamily(MY_CF);
query.setKey(myRowKey);



if(sortOrder == 0)   // Specifying a startPoint and endPoint with reversing = false and limit as number of result to be returned
    query.setRange(startTimePoint,endTimePoint, false, limit);
else if(sortOrder == 1)    // Specifying a startPoint and endPoint with reversing = true and limit as number of result to be returned, note since we want the results to be descending we specify end to start , it should be the other way round for reverseOrder as you can see below
    query.setRange(endTimePoint,startTimePoint, true, limit);


//    query.setRange(long startTime,long endTime, boolean reverseOrder, int limit);




Similarly we can do this for Standard type ColumnFamily using SliceQuery instead of SuperSliceQuery

Hope this was useful . Please leave your comments , if this was useful for you.




4 comments:

  1. it was fine.but i was searching that "how to get the unique records from cassandra".

    ReplyDelete
  2. Hi Rakesh , sorry for replying late ... can you elaborate what you mean by unique records from cassandra ?

    ReplyDelete
  3. I have similar requirement. I have multiple range of ip addresses (low and high value as long) assigned to each country. I need to query the records by providing a single ip (in long format) and find out which county this ip belong to by selecting the range. I am not sure how to achieve this in cassandra. Any help would be appreciated.

    ReplyDelete
    Replies
    1. Hi Thameem ,
      If i understand your question right . you have this schema
      ColumnFamily : IPAddressCF
      RowKey : some constant
      Column Name = {some long value}

      You can do this in two ways , if you know the exact column name . You can do a ColumnQuery.

      If you do a not the exact column , but you know the range in which it falls , you can do a RangeSliceQuery.


      SuperSliceQuery query = HFactory.createSuperSliceQuery(keyspace, ss,
      LongSerializer.get(), ss, ss);

      query.setColumnFamily(IPAddressCF);
      query.setKey(myRowKey);


      query.setRange(IPAddressInLong,0L, true, 1);


      This would return a ColumnSlice which will have only one HColumn that will be equal to the IPAddressInLong if available or the most close IPAddressInLong value to the one specified.

      But, it feels like you need to get the exact column. So you need to use the ColumnQuery

      and do query.setColumnName(IPAddressInLong). This will return a HColumn if available.


      Let me know if it helped



      Thanks
      Arun

      Delete