Getting Started with Java

If you haven’t set up a Riak Node and started it, please visit Running A Cluster first.

To try this flavor of Riak, a working installation of Java is required.

Client Setup

To include the Riak Java client in your project, add it to your project’s dependencies. Here is a Maven example:

<dependencies>
  <dependency>
    <groupId>com.basho.riak</groupId>
    <artifactId>riak-client</artifactId>
    <version>2.1.1</version>
  </dependency
</dependencies>

Next, download TasteOfRiak.java source code for this tutorial, and save it to your working directory.

Configuring for a local cluster
The `TasteOfRiak.java` file that you downloaded is set up to communicate with a 1-node Riak cluster listening on `localhost` port 10017. We recommend modifying the connection info directly within the `setUpCluster()` method.

If you execute the TasteOfRiak.java file within your IDE, you should see the following:

Basic object created
Location object created for quote object
StoreValue operation created
Client object successfully created
Object storage operation successfully completed
Success! The object we created and the object we fetched have the same value
Quote object successfully deleted
Book object created
Moby Dick information now stored in Riak
Book object successfully fetched
Success! All of our tests check out

Since Java doesn’t have a REPL environment, let’s walk through the code to see what it actually did at each step.

Setting Up the Cluster

The first step in using the Riak Java client is to create a cluster object to facilitate all interactions with Riak. You’ll see this on line 72:

RiakCluster cluster = setUpCluster();

This calls the private setUpCluster method which begins on line 25. Using that cluster object, we can instantiate a client object which will execute all Riak interactions:

RiakClient client = new RiakClient(cluster);

Next Steps

CRUD Operations