Skip to main content

A Vert.x client allowing applications to interact with a Cassandra service.

This module has Tech Preview status, this means the API can change between versions.

Using the Cassandra Client for Vert.x

To use this module, add the following to the dependencies section of your Maven POM file:


Or, if you use Gradle:

compile 'io.vertx:vertx-cassandra-client:3.6.3'
the Cassandra client is not compatible with the Vert.x Dropwizard Metrics library. Both are using a different major version of the Dropwizard Metrics library and the Cassandra driver won’t upgrade to the most recent version due to the drop of Java 7. The next major version of the Cassandra driver (4) will uses a more recent Dropwizard Metrics library version.

Creating a client

Cassandra is a distributed system, and it can have many nodes. To connect to Cassandra you need to specify the addresses of some cluster nodes when creating a CassandraClientOptions object:

CassandraClientOptions options = new CassandraClientOptions()
CassandraClient client = CassandraClient.createShared(vertx, options);
By default, the Cassandra client for Vert.x will connect to the local machine’s port 9042.


After the client is created you can connect using specific cluster options:

cassandraClient.connect(connect -> {
  if (connect.succeeded()) {
    System.out.println("Just connected");
  } else {
    System.out.println("Unable to connect");

To disconnect, you can do it in a similar way:

cassandraClient.disconnect(disconnect -> {
  if (disconnect.succeeded()) {
    System.out.println("Just disconnected");
  } else {
    System.out.println("Unable to disconnect");

Using the API

The client API is represented by CassandraClient.


You can get query results using three different ways.


The streaming API is most appropriate when you need to consume results iteratively, e.g you want to process each item. This is very efficient specially for large amount of rows.

In order to give you some inspiration and ideas on how you can use the API, we’d like to you to consider this example:

cassandraClient.queryStream("SELECT my_string_col FROM my_keyspace.my_table where my_key = 'my_value'", queryStream -> {
  if (queryStream.succeeded()) {
    CassandraRowStream stream = queryStream.result();

    // resume stream when queue is ready to accept buffers again
    response.drainHandler(v -> stream.resume());

    stream.handler(row -> {
      String value = row.getString("my_string_col");

      // pause row stream when we buffer queue is full
      if (response.writeQueueFull()) {

    // end request when we reached end of the stream
    stream.endHandler(end -> response.end());

  } else {
    // response with internal server error if we are not able to execute given query
      .end("Unable to execute the query");

In the example, we are executing a query, and stream results via HTTP.

Bulk fetching

This API should be used when you need to process all the rows at the same time.

cassandraClient.executeWithFullFetch("SELECT * FROM my_keyspace.my_table where my_key = 'my_value'", executeWithFullFetch -> {
  if (executeWithFullFetch.succeeded()) {
    List<Row> rows = executeWithFullFetch.result();
    for (Row row : rows) {
      // handle each row here
  } else {
    System.out.println("Unable to execute the query");
Use bulk fetching only if you can afford to load the full result set in memory.

Low level fetch

This API provides greater control over loading at the expense of being a bit lower-level than the streaming and bulk fetching APIs.

cassandraClient.execute("SELECT * FROM my_keyspace.my_table where my_key = 'my_value'", execute -> {
  if (execute.succeeded()) {
    ResultSet resultSet = execute.result(); -> {
      if (one.succeeded()) {
        Row row = one.result();
        System.out.println("One row successfully fetched");
      } else {
        System.out.println("Unable to fetch a row");

    resultSet.fetchMoreResults(fetchMoreResults -> {
      if (fetchMoreResults.succeeded()) {
        int availableWithoutFetching = resultSet.getAvailableWithoutFetching();
        System.out.println("Now we have " + availableWithoutFetching + " rows fetched, but not consumed!");
        if (resultSet.isFullyFetched()) {
          System.out.println("The result is fully fetched, we don't need to call this method for one more time!");
        } else {
          System.out.println("The result still does not fully fetched");
      } else {
        System.out.println("Unable to fetch more results");

  } else {
    System.out.println("Unable to execute the query");

Prepared queries

For security and efficiency reasons, it is a good idea to use prepared statements for all the queries you are using more than once.

You can prepare a query:

cassandraClient.prepare("SELECT * FROM my_keyspace.my_table where my_key = ? ", preparedStatementResult -> {
  if (preparedStatementResult.succeeded()) {
    System.out.println("The query has successfully been prepared");
    PreparedStatement preparedStatement = preparedStatementResult.result();
    // now you can use this PreparedStatement object for the next queries
  } else {
    System.out.println("Unable to prepare the query");

And then use the PreparedStatement for all the next queries:

cassandraClient.execute(preparedStatement.bind("my_value"), done -> {
  ResultSet results = done.result();
  // handle results here

// Bulk fetching API
cassandraClient.executeWithFullFetch(preparedStatement.bind("my_value"), done -> {
  List<Row> results = done.result();
  // handle results here

// Streaming API
cassandraClient.queryStream(preparedStatement.bind("my_value"), done -> {
  CassandraRowStream results = done.result();
  // handle results here


In case you’d like to execute several queries at once, you can use BatchStatement for that:

BatchStatement batchStatement = new BatchStatement()
  .add(new SimpleStatement("INSERT INTO NAMES (name) VALUES ('Pavel')"))
  .add(new SimpleStatement("INSERT INTO NAMES (name) VALUES ('Thomas')"))
  .add(new SimpleStatement("INSERT INTO NAMES (name) VALUES ('Julien')"));

cassandraClient.execute(batchStatement, result -> {
  if (result.succeeded()) {
    System.out.println("The given batch executed successfully");
  } else {
    System.out.println("Unable to execute the batch");

RxJava 2 API

The Cassandra client provides an Rxified version of the original API.

Creating an Rxified client

To create an Rxified Cassandra client, make sure to import the CassandraClient class. Then use one of the create methods to get an instance:

CassandraClientOptions options = new CassandraClientOptions()
CassandraClient cassandraClient = CassandraClient.createShared(vertx, options);

Before querying, connect to the cluster:

cassandraClient.rxConnect().subscribe(() -> {
  // Connected succesfully
}, throwable -> {
  // Handle failure


In this section, we will reconsider some of the previous use cases with the Rxified API.


A CassandraRowStream can be converted to a Flowable, which is handy when you have to deal with large data sets:

cassandraClient.rxQueryStream("SELECT my_key FROM my_keyspace.my_table where my_key = my_value")
  // Convert the stream to a Flowable
  .subscribe(row -> {
    // Handle single row
  }, t -> {
    // Handle failure
  }, () -> {
    // End of stream

Bulk fetching

When your data set is small, it might be easier to get all results at once:

cassandraClient.rxExecuteWithFullFetch("SELECT my_key FROM my_keyspace.my_table where my_key = my_value")
  .subscribe(rows -> {
    // Handle list of rows
  }, throwable -> {
    // Handle failure