RethinkDB at Compose: Java and SSL


Last December, RethinkDB announced that they had added Java to their list of officially supported drivers. For Compose users there was one tiny snag though; the new drivers didn't support SSL. That's changed this year thanks to the work of Paulo Pires who added a Compose compatible SSL implementation to the RethinkDB Java driver. So, without further ado, here's how to use it.

First stop, installing the Java Driver. The RethinkDB instructions cover how to use the most common Java package and build management systems – Maven, Gradle, Ant. The one change from the instructions, as of writing this, is you'll need to change the version to 2.2-beta-2 in whichever configuration file you choose. We're building with Gradle, so our whole build.gradle file looks like this:

apply plugin: 'java'  
apply plugin: 'application'  
mainClassName = 'rethinkjava.RethinkJava'

repositories {  

dependencies {  
    compile group: 'com.rethinkdb', name: 'rethinkdb-driver', version: '2.2-beta-2'
    compile 'log4j:log4j:1.2.17'
    compile 'org.slf4j:slf4j-api:1.7.5'
    compile 'org.slf4j:slf4j-log4j12:1.7.5'

We're then creating a src/main/java/rethinkjava directory where we'll put our code: As always we start with the package and imports from libraries, both system and third-party:

package rethinkjava;

import com.rethinkdb.RethinkDB;  
import com.rethinkdb.gen.exc.ReqlError;  
import com.rethinkdb.gen.exc.ReqlQueryLogicError;  
import com.rethinkdb.model.MapObject;  

import java.util.ArrayList;  

And with that pre-amble out of the way we can move to create our example's code. We're packaging all this up as a static method inside a class called RethinkJava for simplicity. You would, of course, integrate it with your own framework. Let's get that class defined:

public class RethinkJava {  
    public static final RethinkDB r = RethinkDB.r;

Ok, here's where RethinkDB's driver gets its top-level namespace from. It's a single point of reference for all of the commands and functions that make up the API. It may be considered somewhat not of the Java idiom but remember that the API is designed to work like the JavaScript, Ruby and Python APIs. With that in mind, don't go looking for the RethinkDBConcreteAbstractAPIFactory or similar; this is an API which is inspired by dynamic, fluent language styles. Practically, we just create a final variable r from the RethinkDB libraries r and we're good to go.

For the next part, we'll need to pop over to the Compose console to get the connection details from there...

We need to get some information from this page. The host and port are embedded in the connection string, the host is after the @ and ends at the colon while the port follows that. We'll add those to our code:

    public static final String host = "";
    public static final int port = 10965;

Next we'll need the Auth Key. You can reveal that by clicking on the show/change button. That'll give you a string which we can add into our code too:

    public static final String authKey="GbDfAcEGbDfAcEGbDfAcEGbDfAcEGbDfAcE";

The final thing we need is the certificate file to validate the connection. Click on the Show SSL Public Key and copy the certificate which is displayed into a local file. We copied it into certfile.crt and then added a reference to that to our code:

    public static final String certfile = "./certfile.crt";

Now we can create our SSL connection in our main method:

    public static void main(String[] args) {
        System.out.println("Welcome to RethinkJava on Compose");

        try {
          FileInputStream cert = new FileInputStream(certfile);

          Connection conn = r.connection().

We've wrapped the active code in a try...catch block to pick up all exceptions. First line inside the try block is creating a FileInputStream which reads from that SSL certificate we copied; if the file name is bad it'll bounce out with a FileNotFoundException. Now we can create the Connection. Again we're going to remind you that the RethinkDB Java API takes it's stylings from the fluent API of other languages.

That means, when we go to create the connection, we do it by chaining methods together. We call the r variable's connection() method, then the hostname() method passing the host name, port() passing the port number, authKey() passing the auth key, certFile() passing the FileInputStream for the certificate file and finally, we call .connect() on the result of all those setting methods to get it to connect.

If that connection doesn't work, we'll be bounced out in an exception here too. Assuming it has worked, we can make a call on the RethinkDB database:

       ArrayList<String> tables = r.db("example").tableList().run(conn);
       for (String tablename:tables)

The same chaining is in play here. We ask r for a reference to a database called example with .db("example") and then call the tableList() method on that. To make that sequence do something, we ask the .run method to execute it using the connection we just created. The rest of that code just prints out the names of the tables we get in return.

It's a lovely implementation of the RethinkDB API in Java and with the addition of SSL support, Compose users can start making use of it in their applications. You can read more about the general use of the Java driver API in the official documentation and API reference. You'll find all the code in this example in our examples repository on GitHub. There's also handy Rethink blog postings with demonstrations of using the Java driver with vert.x and with other JVM languages.

Dj Walker-Morgan
Dj Walker-Morgan was Compose's resident Content Curator, and has been both a developer and writer since Apples came in II flavors and Commodores had Pets. Love this article? Head over to Dj Walker-Morgan’s author page to keep reading.

Conquer the Data Layer

Spend your time developing apps, not managing databases.