Skip to content

Reading Data

elandau edited this page Nov 29, 2012 · 7 revisions

Query a single column

Column<String> result = keyspace.prepareQuery(CF_STANDARD1)
    .getKey(rowKey)
    .getColumn("Column1")
    .execute().getResult();
String value = result.getStringValue();

Query an entire row

ColumnList<String> result = keyspace.prepareQuery(CF_STANDARD1)
    .getKey(rowKey)
    .execute().getResult();
if (!result.isEmpty()) {
   ...
}

Paginate through all columns in a row

ColumnList<String> columns;
int pageize = 10;
try {
	RowQuery<String, String> query = keyspace
		.prepareQuery(CF_STANDARD1)
		.getKey("A")
		.setIsPaginating()
		.withColumnRange(new RangeBuilder().setMaxSize(pageize).build());

	while (!(columns = query.execute().getResult()).isEmpty()) {
		for (Column<String> c : columns) {
		}
	}
} catch (ConnectionException e) {
}

Query all with callback

This query breaks up the keys into token ranges and queries each range in a separate thread.

keyspace.prepareQuery(CF_STANDARD1)
    .getAllRows()
    .setRowLimit(100)  // Read in blocks of 100
    .setRepeatLastToken(false)
    .withColumnRange(new RangeBuilder().setLimit(2).build())
    .executeWithCallback(new RowCallback<String, String>() {
        @Override
        public void success(Rows<String, String> rows) {
            // Do something with the rows that were fetched.  Called once per block.
        }

        @Override
        public boolean failure(ConnectionException e) {
            return true;  // Returning true will continue, false will terminate the query
        }
    });

Counting number of columns in a response

Cassandra provides an API to count the number of columns in a reponse without returning the query data.  This is not a constant time operation because Cassandra actually has to read the row and count the columns.  This will be optimized in a future version.

int count = keyspace.prepareQuery(CF_STANDARD1)
    .getKey(rowKey)
    .getCount()
    .execute().getResult();

Column slice queries

Use a column slice to narrow down the range of columns returned in a query.  A column slice can be added to any of the queries by calling setColumnSlice on the query object prior to calling execute().   Columns slices come in two flavors, column slice and column range. Use wtihColumnSlice to return a non-contiguous set of columns. Use withColumnRange to return an ordered range of slices.

This is the general format of a column slice.

ColumnList<String> result;
result = keyspace.prepareQuery(CF_STANDARD1)
   .getKey(rowKey)
   .withColumnRange(new RangeBuilder().setStart("firstColumn").setEnd("lastColumn").setMaxSize(100).build())
   .execute().getResult();
if (!result.isEmpty()) {
    ...
}

Query columns with prefix

Let's assume you have data that looks like this,

CF_STANDARD1:{
    "Prefixes":{
        "Prefix1_a":1,
        "Prefix1_b":2,
        "Prefix2_a":3,
    }
}

To get a slice of columns that start with "Prefix1", perform the following query

OperationResult<ColumnList<String>> r = keyspace.prepareQuery(CF_STANDARD1)
    .getKey("Prefixes")
    .withColumnRange(new RangeBuilder()
        .setStart("Prefix1_\u00000")
        .setEnd("Prefix1_\uffff")
        .setLimit(Integer.MAX_VALUE).build())
    .execute();

Query for first 5 columns

OperationResult<ColumnList<String>> r = keyspace.prepareQuery(CF_STANDARD1)
    .getKey(rowKey)
    .withColumnRange(new RangeBuilder().setMaxSize(5).build())
    .execute();

Query for last N columns

OperationResult<ColumnList<String>> r = keyspace.prepareQuery(CF_STANDARD1)
    .getKey(rowKey)
    .withColumnRange(new RangeBuilder().setReversed().setLimit(5).build())
    .execute();

Query for specific column names

Use this type of column slice when you have fixed column names.

OperationResult<ColumnList<String>> r = keyspace.prepareQuery(CF_STANDARD1)
    .getKey(rowKey)
    .withColumnSlice("First", "Last", "Age")
    .execute();
Clone this wiki locally