Install and Start Using the Java SDK with Couchbase Server

The Couchbase Java SDK allows Java applications to access a Couchbase cluster. It offers synchronous APIs as well as reactive and asynchronous equivalents to maximize flexibility and performance.

The Couchbase Java SDK 3.0 is a complete rewrite of the 2.x API, providing a simpler surface area and adding support for future Couchbase Server features like Collections and Scopes (available in Couchbase Server 6.5 as a developer preview). The (reactive) API also migrated from RxJava to Reactor, along with other improvements to performance, logging, debugging and timeout troubleshooting. If you’re upgrading your application from Java SDK 2.x, please read our Migrating 2.x code to SDK 3.0 Guide.

Installing the SDK

At least Java 8 is required for current releases; see the Compatibility section for details. We recommend running the latest Java LTS version (i.e. at the time of writing JDK 11) with the highest patch version available.

Couchbase publishes all stable artifacts to Maven Central. You can use your favorite dependency management tool to install the SDK. The following snippet shows how to do it with maven.

<dependencies>
    <dependency>
        <groupId>com.couchbase.client</groupId>
        <artifactId>java-client</artifactId>
        <version>3.0.1</version>
    </dependency>
</dependencies>

For gradle, you can use:

implementation 'com.couchbase.client:java-client:3.0.1'

Alternatively, we provide a zip file with all the dependencies bundled if you wish to manually include the jar files in your classpath. Refer to the Release Notes for further details. You can also find links to the hosted javadocs there.

Hello Couchbase

Once you have the Java client installed, open your IDE, and try out the following:

Cluster cluster = Cluster.connect("localhost", "username", "password");

Couchbase uses Role Based Access Control (RBAC) to control access to resources. Here we will use the Full Admin role created during installation of the Couchbase Data Platform. For production client code, you will want to use more appropriate, restrictive settings — but here we want to get you up and running quickly. If you’re developing client code on the same VM or machine as the Couchbase Server, your connection string can be just localhost.

The Cluster provides access to cluster-level operations like N1Ql queries, analytics or full-text search. You will also find different management APIs on it.

If you are not using an IDE or are new to java, the following imports are necessary to build the following snippets:

import com.couchbase.client.java.*;
import com.couchbase.client.java.kv.*;
import com.couchbase.client.java.json.*;
import com.couchbase.client.java.query.*;

To access the KV (Key/Value) API or to query views, you need to open a Bucket:

// get a bucket reference
Bucket bucket = cluster.bucket("bucket-name");

If you installed the travel-sample` data bucket, substitute travel-sample for bucket-name.

The 3.0 SDK is ready for the introduction of Collections in an upcoming release of the Couchbase Data Platform. The latest release, Couchbase Server 6.5, brings a limited Developer Preview of Collections, allowing Documents to be grouped by purpose or theme, according to specified Scope. Here we will use the defaultCollection, which covers the whole Bucket and must be used when connecting to a 6.5 cluster or earlier.

// get a collection reference
Collection collection = bucket.defaultCollection();

KV Operations are described in detail on the KV Operations page, but to get you started the following code creates a new document and then fetches it again, printing the result.

// Upsert Document
MutationResult upsertResult = collection.upsert(
    "my-document",
    JsonObject.create().put("name", "mike")
);

// Get Document
GetResult getResult = collection.get("my-document");
System.out.println(getResult);

You can also perform a N1QL query at the cluster level:

QueryResult result = cluster.query("select \"Hello World\" as greeting");
System.out.println(result.rowsAsObject());

You can learn more about N1QL queries on the Query page. Other services (like analytics, search or views) work very similar to the two shown above. Please refer to their respective documentation sections to learn more.

Full Example

If you want to copy and paste to run the full example, here it is:

import com.couchbase.client.java.*;
import com.couchbase.client.java.kv.*;
import com.couchbase.client.java.json.*;
import com.couchbase.client.java.query.*;

class StartUsing {

    public static void main(String... args) {
        Cluster cluster = Cluster.connect("localhost", "username", "password");
        Bucket bucket = cluster.bucket("bucket-name");
        Collection collection = bucket.defaultCollection();

        MutationResult upsertResult = collection.upsert(
            "my-document",
            JsonObject.create().put("name", "mike")
        );
        GetResult getResult = collection.get("my-document");
        System.out.println(getResult);

        QueryResult result = cluster.query("select \"Hello World\" as greeting");
        System.out.println(result.rowsAsObject());
    }

}

Additional Resources

The API reference is generated for each release and can be found in the Release Notes.

Couchbase welcomes community contributions to the Java SDK. The Java SDK source code is available on GitHub.