-
Notifications
You must be signed in to change notification settings - Fork 354
Reading Data
Column<String> result = keyspace.prepareQuery(CF_STANDARD1)
.getKey(rowKey)
.getColumn("Column1")
.execute().getResult();
String value = result.getStringValue();
ColumnList<String> result = keyspace.prepareQuery(CF_STANDARD1)
.getKey(rowKey)
.execute().getResult();
if (!result.isEmpty()) {
...
}
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) {
}
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
}
});
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();
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()) {
...
}
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();
OperationResult<ColumnList<String>> r = keyspace.prepareQuery(CF_STANDARD1)
.getKey(rowKey)
.withColumnRange(new RangeBuilder().setMaxSize(5).build())
.execute();
OperationResult<ColumnList<String>> r = keyspace.prepareQuery(CF_STANDARD1)
.getKey(rowKey)
.withColumnRange(new RangeBuilder().setReversed().setLimit(5).build())
.execute();
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();
A Netflix Original Production
Tech Blog | Twitter @NetflixOSS | Jobs
- Getting-Started
- Configuration
- Features
- Monitoring
- Thread Safety
- Timeouts
- Recipes
- Examples
- Javadoc
- Utilities
- Cassandra-Compatibility
- FAQ
- End-to-End Examples
- Astyanax Integration with Java Driver