Retrieve all Columns of a row in Cassandra using Hector Client

Jignesh Dhua picture Jignesh Dhua · Apr 11, 2012 · Viewed 7.8k times · Source

I'm new in cassandra and hector.

I want to retrieve all columns of a row in Cassandra using hector. After some exploration on web i got some sample codes, but it requires range or column name, like,

SliceQuery<String,String,String> query = HFactory.createSliceQuery(keyspace, ss, ss, ss);
    query.setColumnFamily("MyColumnFamily").setKey("S001").setRange(null, null, false, 100);
    QueryResult<ColumnSlice<String,String>> result = query.execute();
    for (HColumn<String, String> column : result.get().getColumns()) {
        System.out.println(column.getName() +"::"+ column.getValue());
    }

Here i need to set Range in setRange() method which retrieves columns in this range. We can also set start and end in this method, but this will also give columns of particular range. If i'm not setting range then I need to give Column names as array.

Is there any way to get all columns of particular row? I don't want to set range or column names, I just want all columns of a row. because in my application columns are not predefined.

Or is there any way to get total column count of a row so i can set it in setRange() method?

Thanks.

Answer

nickmbailey picture nickmbailey · Apr 11, 2012

The way to do this for somewhat small rows is using the setRange() method exactly how it is being used in the example you pasted. If you set the start and end parameters of the range to null, then the range to fetch from is the entire row.

The only thing you need to account for at that point is the limit. In the example you pasted the limit is set to 100, so the query will not return more than 100 columns. You can set the limit to a very large number (larger than the possible number of columns you would have) if you want to always retrieve all of the columns in a row but this is not generally a good idea.

Instead you probably want to create a hector ColumnSliceIterator, for the range query, which will give you an iterator interface for the row and allow you to iterate through the entire row without querying for too many columns at once. See the example under 'Column Iteration' at the bottom of this page:

https://github.com/rantav/hector/wiki/Getting-started-%285-minutes%29