Search code examples
cassandraastyanax

how to read all 1000 rows from cassandra CF with astyanax


We have this one CF that only has about 1000 rows. Is there a way with astyanax to read all 1000 rows? Does thrift even support that?

thanks, Dean


Solution

  • You can read all rows with the thrift call get_range_slices. Note that it returns rows in token order, not key order. So it's fine to read all the rows but not to do ranges across row keys.

    You can use it in Astyanax with the getAllRows(). Here is some sample code (copied from the docs at https://github.com/Netflix/astyanax/wiki/Reading-Data#iterate-all-rows-in-a-column-family)

    Rows<String, String>> rows;
    try {
        rows = keyspace.prepareQuery("ColumnFamilyName")
            .getAllRows()
            .setBlockSize(10)
            .withColumnRange(new RangeBuilder().setMaxSize(10).build())
            .setExceptionCallback(new ExceptionCallback() {
                 @Override
                 public boolean onException(ConnectionException e) {
                     try {
                         Thread.sleep(1000);
                     } catch (InterruptedException e1) {
                     }
                     return true;
                 }})
            .execute().getResult();
    } catch (ConnectionException e) {
    }
    
    // This will never throw an exception
    for (Row<String, String> row : rows.getResult()) {
        LOG.info("ROW: " + row.getKey() + " " + row.getColumns().size());
    }
    

    This will return the first 10 columns of each row, in batches of 10 rows. Increase the number passed to RangeBuilder().setMaxSize to get more (or fewer) columns.