Under the hood

RethinkDB releases official Java client driver

Michael Glukhovsky

RethinkDB announced the release of an official Java client driver. The library implements RethinkDB’s query language and lets developers send queries to a RethinkDB cluster. Java developers can use the new client driver with RethinkDB to build real-time web applications, sending queries to the database and receiving live updates as the output changes.

Over the past few years, shifting trends and new technologies changed the way that developers build web applications. Instead of traditional server-side page rendering, we frequently see rich clients powered by backend APIs. The popularity of application-like single-page web interfaces is responsible for driving presentation logic to the frontend. Building on that trend, developers increasingly use persistent WebSocket connections and real-time messaging to imbue web applications with the kind of responsiveness and immediate updates that were previously only possible to achieve in native applications.

Of course, backend web frameworks also evolve alongside frontend innovation. In the Java ecosystem, we’ve seen the popularization of reactive and event-driven programming models as developers adopt compelling libraries like Vert.x and RxJava. RethinkDB’s approach to data persistence and real-time application development–pushing live updates to the frontend–are a great fit for Java stacks that take advantage of these trends.

Introducing the RethinkDB Java driver

RethinkDB’s ReQL query language embeds itself in the underlying programming language that you use to write your application. Instead of cobbling together queries with error-prone string concatenation, you can write queries in your native programming language with a set of APIs provided by the client library.

You chain together methods from the API to express the operations that you want to perform in the database. If you’ve ever used RxJava, the chaining pattern and some of the method names will look very familiar. The following is a simple example that demonstrates how to use the new Java client library:

RethinkDB r = RethinkDB.r;
Connection conn = r.connection().hostname("localhost").connect();

// Find the odd numbers, multiply each by 2, and add the total
Long output = r.range(10)
               .filter(x -> x.mod(2).eq(1))
               .map(x -> x.mul(2)).sum().run(conn);

Under the hood, the RethinkDB library evaluates those expressions into a query term tree, which it translates into the RethinkDB wire protocol. It sends the query to the database and parses the response. As you can see, some of the query language methods take advantage of the new lambda syntax introduced in Java 8.

To get live updates when the contents of the database change, you can attach the changes() method to the query chain. The changes() method creates a changefeed, a cursor that you can iterate over to get updated records. You can wire up a changefeed to broadcast live updates to end users over a WebSocket connection.

For example, the following code shows how to use the Vert.x event bus to send new messages from a chat application to a frontend client in the browser:

EventBus bus = vertx.eventBus();

    new BridgeOptions().addOutboundPermitted(
      new PermittedOptions().setAddress("chat"))));

new Thread(() -> {
  Connection conn = null;

  try {
    conn = r.connection().connect();
    Cursor<HashMap> cur = r.db("chat").table("messages").changes()

    for (HashMap item : cur)
      bus.publish("chat", new JsonObject(item));
  catch (Exception e) {
    System.err.println("Error: changefeed failed");
  finally {

On the frontend, you could use Vert.x’s JavaScript event bus client library in the web browser to receive the incoming messages and display them to the user. You can see check out this repository on GitHub to see a complete sample chat application built with RethinkDB and Vert.x.

Using the RethinkDB Java driver in other JVM languages

The Java client driver can also serve as a starting point for developers who want to use RethinkDB in other popular programming languages that target the JVM. It works out of the box in languages like Groovy and Kotlin that interoperate well with Java and support Java 8 features like lambdas. In Scala, you will want to use the -Xexperimental flag in order to enable Java 8 interop. You can refer to this guide for more details about how the library works in various JVM languages.

Michael Glukhovsky

Michael Glukhovsky

All Posts by Michael Glukhovsky

Michael Glukhovsky is co-founder founder of RethinkDB, an open-source database for building real-time web applications. He's passionate about user experience and building infrastructure software. Prior to RethinkDB Michael attended Stony Brook University and specialized in human-computer interaction.

comments powered by Disqus