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()
.addContactPoint("node1.corp.int", 7000)
.addContactPoint("node2.corp.int", 7000)
.addContactPoint("node3.corp.int", 7000);
CassandraClient cassandraClient = CassandraClient.createShared(vertx, options);
Querying
In this section, we will reconsider some of the previous use cases with the Rxified API.
Streaming
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
.flatMapPublisher(CassandraRowStream::toFlowable)
.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
});