Skip to content
This repository

Maven artifacts

As of Astyanax 1.56.27 the project has been split into multiple sub projects, each of which needs to be pulled in separately.

Required artifacts

GroupID/Org ArtifactID/Name astyanax-core astyanax-thrift astyanax-cassandra

Optional artifacts

GroupID/Org ArtifactID/Name astyanax-queue astyanax-entity-mapper astyanax-recipes

Building Astyanax from source

git clone
cd astyanax
./gradlew build

Initializing Astyanax

To start using Astyanax you must first create a context that captures the connection pool, API and monitoring for your Cassandra client. Default implementations for these components are provided, but you can provide your own. Configuration and monitoring are some of the components that you will most likely want to implement to bind to your company’s specific implementations.

AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
    .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()      
    .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
    .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())

Keyspace keyspace = context.getClient();

If using Cassandra 1.2 and CQL then you will need to tell Astyanax about it. For now, Astyanax has defaults for Cassandra 1.1

AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
    .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()      

Define your column family structure

Cassandra internally stores all keys, columns and values as byte arrays. Astyanax makes use of serializers to convert to and from various primitives and more complex data types. To avoid having to specify serializers for every call you must first set up the column family definition. This definition will most likely be a static final in your DAO or other higher level wrapper. Notice that the definition is at the column family and not the keyspace level. This allows you to have different key types for column families within the same keyspace.

ColumnFamily<String, String> CF_USER_INFO =
  new ColumnFamily<String, String>(
    "Standard1",              // Column Family Name
    StringSerializer.get(),   // Key Serializer
    StringSerializer.get());  // Column Serializer

Inserting data

Data may be inserted either one column at a time or in batches. You may combine updates to multiple column families in the same keyspace into a single batch.

// Inserting data
MutationBatch m = keyspace.prepareMutationBatch();

m.withRow(CF_USER_INFO, "acct1234")
  .putColumn("firstname", "john", null)
  .putColumn("lastname", "smith", null)
  .putColumn("address", "555 Elm St", null)
  .putColumn("age", 30, null);

m.withRow(CF_USER_STATS, "acct1234")
  .incrementCounterColumn("loginCount", 1);

try {
  OperationResult<Void> result = m.execute();
} catch (ConnectionException e) {

Reading data from Cassandra

Astyanax queries begin with a single interface and guide you through the possible query options in a natural flow starting with different key queries (single, slice, range) followed by column level qualifiers. Once the query is ready, you may call several different execute() methods which provide different internal implementations for parallelizing the query execution on the client. Each query will return the proper level of information (Rows, Row, ColumnList, Column, Count) with the appropriate types derived from the ColumnFamily definition object.

Reading a single row

OperationResult<ColumnList<String>> result =
ColumnList<String> columns = result.getResult();

// Lookup columns in response by name 
int age        = columns.getColumnByName("age").getIntegerValue();
long counter   = columns.getColumnByName("loginCount").getLongValue();
String address = columns.getColumnByName("address").getStringValue();

// Or, iterate through the columns
for (Column<String> c : result.getResult()) {

Reading a set of non-contiguous rows

OperationResult<Rows<String, String>> result =
    .getKeySlice("Key1", "Key2", "Key3")

// Iterate rows and their columns 
for (Row<String, String> row : result.getResult()) {
  for (Column<String> column : row.getColumns()) {

Composite columns

Astyanax provides a simple annotation based definition for composite columns. The ordinal annotation attribute is necessary to guarantee the order in which the composite components are serialized.

// Annotated composite class
Class SessionEvent{
  private @Component(ordinal=0) String   sessiondId;
  private @Component(ordinal=1) UUID timestamp;
  public SessionEvent() {
  public int hashCode() { ... }
  public boolean equals(Object o) { ... }
  public int compareTo(Object o) { ... }

static AnnotatedCompositeSerializer<SessionEvent> eventSerializer
      = new AnnotatedCompositeSerializer<SessionEvent>(SessionEvent.class);
static ColumnFamily<String, SessionEvent> CF_SESSION_EVENTS
  = new ColumnFamily<String, SessionEvent>("SessionEvents", 
    StringSerializer.get(), eventSerializer);

// Querying cassandra for all columns of row key "SomeUserName" starting with 
      eventSerializer.makeEndpoint("sessionid1", Equality.EQUAL).toBytes(),
      eventSerializer.makeEndpoint("sessionid1", Equality.LESS_THAN_EQUAL).toBytes(),
      false, 100)


Pagination is done transparently by calling autoPaginate(true) where appropriate. It is possible to paginate key ranges (all using the same non paginating column slice), index queries and columns of a single row. Astyanax handles all of the internal details of how to determine the next rowkey or column name without returning duplicates to the caller.

ColumnList<String> columns;
try {
    RowQuery<String, String> query = keyspace
        .withColumnRange(new RangeBuilder().setLimit(10).build());
    while (!(columns = query.execute().getResult()).isEmpty()) {
        for (Column<String> c : columns) {
} catch (ConnectionException e) {

Iterate through the entire keyspace (deprecated)

Use the AllRowsReader recipe instead

This is arguably a very bad use case of Cassandra, but one that is very useful for column families that store a small number of rows. Astyanax provides two methods for executing this type of query. The first, wraps the iterator interface and transparently queries cassandra when .next() is called. The second queries each token range from a separate thread and returns the rows via a callback interface.

try {
    OperationResult<Rows<String, String>> rows = keyspace.prepareQuery(CF_STANDARD1)
	.setRowLimit(100)  // This is the page size
	.withColumnRange(new RangeBuilder().setMaxSize(10).build())
	.setExceptionCallback(new ExceptionCallback() {
            public boolean onException(ConnectionException e) {
                return true;
    for (Row<String, String> row : rows.getResult()) {"ROW: " + row.getKey() + " " + row.getColumns().size());
} catch (ConnectionException e) {;

The ExceptionCallback is necessary since the Iterator cannot throw a checked exception. The callback gives the caller the opportunity to force Astyanax to retry the query.

    .setRowLimit(100)  // This is the page size
    .withColumnRange(new RangeBuilder().setLimit(2).build())
    .executeWithCallback(new RowCallback<String, String>() {
        public void success(Rows<String, String> rows) {
            for (Row<String, String> row : rows) {
                System.out.println("ROW: " + row.getKey() + " " + row.getColumns().size());
        public boolean failure(ConnectionException e) {
            return false;

Range Builder

The range builder simplifies specifying the start and end columns for a column range query.

See note on thread-safety

Astyanax application code examples

Compilable code examples can be found in the project repository astyanax-examples.

Something went wrong with that request. Please try again.