Search code examples
javacassandrahector

Cassandra Hector : How to retrieve all keyrows value in family?


I am newbie in casssandra and wants to fetch all rowkey value from CloumnFamily. suppose I have User CoulmnFamily look like this

list User

RowKey: amit
=> (column=fullname, value=amitdubey, timestamp=1381832571947000)
-------------------
RowKey: jax1
=> (column=fullname, value=jagveer1, timestamp=1381655141564000)
-------------------
RowKey: jax2
=> (column=fullname, value=amitdubey, timestamp=1381832571947000)
-------------------
RowKey: jax3
=> (column=fullname, value=jagveer3, timestamp=1381655141564000)
-------------------

I am looking for the example code to retrieve the all keyrows value of the family.

Something like this:

amit
jax1
jax2
jax3

My Cassandra Version is 1.1 appreciate any help


Solution

  • You can do it simply by using RangeSlicesQuery in hector client.

    RangeSlicesQuery<String, String, String> rangeSlicesQuery =
                HFactory.createRangeSlicesQuery(keyspace, ss, ss, ss)
                        .setColumnFamily("User")
                        .setRange(null, null, false, rowCount)
                        .setRowCount(rowCount)
                        .setReturnKeysOnly();
    
        String lastKey = null;
    
        while (true) {
            rangeSlicesQuery.setKeys(lastKey, null);
    
            QueryResult<OrderedRows<String, String, String>> result = rangeSlicesQuery.execute();
            OrderedRows<String, String, String> rows = result.get();
            Iterator<Row<String, String, String>> rowsIterator = rows.iterator();
    
            /**
             * we'll skip this first one, since it is the same as the last one from previous time we executed.
             */
            if (lastKey != null && rowsIterator != null) {
                rowsIterator.next();
            }
            while (rowsIterator.hasNext()) {
                Row<String, String, String> row = rowsIterator.next();
                lastKey = row.getKey();
    
                System.out.println(lastkey);
            }
    
            if (rows.getCount() < rowCount) {
                break;
            }
        }