Start Using the Java SDK

    +
    A quick start guide to get you up and running with Couchbase and the Java SDK.

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

    In this guide, you will learn:

    Hello Couchbase

    We will go through the code sample step by step, but for those in a hurry, here’s the complete code:

    • Couchbase Capella Sample

    • Local Couchbase Server

    If you are connecting to Couchbase Capella, you’ll need to know the endpoint address, as well as a username and password.

    This example requires the Travel Sample Bucket. The Couchbase Capella free trial version comes with this bucket, and its Query indexes, loaded and ready.

    import com.couchbase.client.java.*;
    import com.couchbase.client.java.kv.*;
    import com.couchbase.client.java.json.*;
    import com.couchbase.client.java.query.*;
    
    import java.time.Duration;
    
    public class StartUsingCapella {
      // Update these variables to point to your Couchbase Capella instance and credentials.
      static String connectionString = "couchbases://cb.<your-endpoint-here>.cloud.couchbase.com";
      static String username = "username";
      static String password = "Password!123";
      static String bucketName = "travel-sample";
    
      public static void main(String... args) {
        Cluster cluster = Cluster.connect(
            connectionString,
            ClusterOptions.clusterOptions(username, password).environment(env -> {
              // Sets a pre-configured profile called "wan-development" to help avoid
              // latency issues when accessing Capella from a different Wide Area Network
              // or Availability Zone (e.g. your laptop).
              env.applyProfile("wan-development");
            })
        );
    
        // Get a bucket reference
        Bucket bucket = cluster.bucket(bucketName);
        bucket.waitUntilReady(Duration.ofSeconds(10));
    
        // Get a user-defined collection reference
        Scope scope = bucket.scope("tenant_agent_00");
        Collection collection = scope.collection("users");
    
        // Upsert Document
        MutationResult upsertResult = collection.upsert(
            "my-document",
            JsonObject.create().put("name", "mike")
        );
    
        // Get Document
        GetResult getResult = collection.get("my-document");
        String name = getResult.contentAsObject().getString("name");
        System.out.println(name); // name == "mike"
    
        // Call the query() method on the scope object and store the result.
        Scope inventoryScope = bucket.scope("inventory");
        QueryResult result = inventoryScope.query("SELECT * FROM airline WHERE id = 10;");
    
        // Return the result rows with the rowsAsObject() method and print to the terminal.
        System.out.println(result.rowsAsObject());
      }
    }

    Before running this example, you will need to install the Travel Sample Bucket using either the Web interface or the command line.

    import com.couchbase.client.java.*;
    import com.couchbase.client.java.kv.*;
    import com.couchbase.client.java.json.*;
    import com.couchbase.client.java.query.*;
    
    import java.time.Duration;
    
    public class StartUsing {
      // Update these variables to point to your Couchbase Server instance and credentials.
      static String connectionString = "couchbase://127.0.0.1";
      static String username = "Administrator";
      static String password = "password";
      static String bucketName = "travel-sample";
    
      public static void main(String... args) {
        Cluster cluster = Cluster.connect(
            connectionString,
            ClusterOptions.clusterOptions(username, password).environment(env -> {
              // Customize client settings by calling methods on the "env" variable.
            })
        );
    
        // get a bucket reference
        Bucket bucket = cluster.bucket(bucketName);
        bucket.waitUntilReady(Duration.ofSeconds(10));
    
        // get a user-defined collection reference
        Scope scope = bucket.scope("tenant_agent_00");
        Collection collection = scope.collection("users");
    
        // Upsert Document
        MutationResult upsertResult = collection.upsert(
            "my-document",
            JsonObject.create().put("name", "mike")
        );
    
        // Get Document
        GetResult getResult = collection.get("my-document");
        String name = getResult.contentAsObject().getString("name");
        System.out.println(name); // name == "mike"
    
        // Call the query() method on the scope object and store the result.
        Scope inventoryScope = bucket.scope("inventory");
        QueryResult result = inventoryScope.query("SELECT * FROM airline WHERE id = 10;");
    
        // Return the result rows with the rowsAsObject() method and print to the terminal.
        System.out.println(result.rowsAsObject());
      }
    }

    Quick Installation

    We recommend running the latest Java LTS version (i.e. at the time of writing JDK 17) with the highest patch version available. Couchbase publishes all stable artifacts to Maven Central.

    The latest version (as of February 2023) is 3.4.3.

    You can use your favorite dependency management tool to install the SDK.

    • Maven

    • Gradle

    <dependencies>
        <dependency>
            <groupId>com.couchbase.client</groupId>
            <artifactId>java-client</artifactId>
            <version>3.4.3</version>
        </dependency>
    </dependencies>
    implementation 'com.couchbase.client:java-client:3.4.3'

    See the installation page for more detailed instructions.

    Prerequisites

    The following code samples assume:

    • Couchbase Capella

    • Local Couchbase Server

    • You have signed up to Couchbase Capella.

    • You have created your own bucket, or loaded the Travel Sample dataset. Note, the Travel Sample dataset is installed automatically by the Capella free trial.

    • A user is created with permissions to access the cluster (at least Application Access permissions). See the Capella connection page for more details.

    Couchbase Capella uses Roles to control user access to database resources. For the purposes of this guide, you can use the Organization Owner role automatically assigned to your account during installation of the Capella cluster. In a production scenario, we strongly recommend setting up users with more granular access roles as a best practice.
    • Couchbase Server is installed and accessible locally.

    • You have created your own bucket, or loaded the Travel Sample dataset using the Web interface.

    • A user is created with permissions to access your cluster (at least Application Access permissions). See Manage Users, Groups and Roles for more details.

    Couchbase Server uses Role Based Access Control (RBAC) to control access to resources. In this guide we suggest using the Full Admin role created during setup of your local Couchbase Server cluster. For production client code, you will want to use more appropriate, restrictive settings.

    Step by Step

    Here are all the imports needed to run the sample code:

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

    If you haven’t already, create an empty class and add a main() method.

    public class YourClassName {
        public static void main(String... args) {}
    }

    Make sure to replace YourClassName with your own class name.

    Above the main() method, add the following variables and update them accordingly:

    • Couchbase Capella

    • Local Couchbase Server

    // Update these variables to point to your Couchbase Capella instance and credentials.
    static String connectionString = "couchbases://cb.<your-endpoint-here>.cloud.couchbase.com";
    static String username = "username";
    static String password = "Password!123";
    static String bucketName = "travel-sample";
    // Update these variables to point to your Couchbase Server instance and credentials.
    static String connectionString = "couchbase://127.0.0.1";
    static String username = "Administrator";
    static String password = "password";
    static String bucketName = "travel-sample";

    In the following sections we will populate the main() method.

    Connect

    Connect to your cluster by calling the Cluster.connect() method and pass it your connection details. The basic connection details that you’ll need are given below — for more background information, see Managing Connections.

    • Couchbase Capella

    • Local Couchbase Server

    From version 3.3, the Java SDK includes Capella’s standard Certificate Authority (CA) certificates by default, so you don’t need any additional configuration. Capella requires TLS, which you can enable by using a connection string that starts with couchbases:// (note the final 's').

    This example shows how to connect and customize the Cluster Environment settings.

    Cluster cluster = Cluster.connect(
        connectionString,
        ClusterOptions.clusterOptions(username, password).environment(env -> {
          // Sets a pre-configured profile called "wan-development" to help avoid
          // latency issues when accessing Capella from a different Wide Area Network
          // or Availability Zone (e.g. your laptop).
          env.applyProfile("wan-development");
        })
    );

    When accessing Capella from a different Wide Area Network or Availability Zone, you may experience latency issues with the default connection settings. SDK 3.4 introduces a wan-development Configuration Profile, which provides pre-configured timeout settings suitable for working in high latency environments. Basic usage is shown in the example above, but if you want to learn more see Constrained Network Environments.

    The Configuration Profiles feature is currently a Volatile API and may be subject to change.

    For developing locally on the same machine as Couchbase Server, your connection string can be couchbase://127.0.0.1 as shown here. For production deployments, you will want to enable TLS by using couchbases:// (note the final 's') instead of couchbase://.

    This example shows how to connect and customize the Cluster Environment settings.

    Cluster cluster = Cluster.connect(
        connectionString,
        ClusterOptions.clusterOptions(username, password).environment(env -> {
          // Customize client settings by calling methods on the "env" variable.
        })
    );
    Simpler Connection

    There’s also a simpler version of Cluster.connect() for when you don’t need to customize the cluster environment:

    // Alternatively, connect without customizing the cluster envionrment.
    Cluster cluster = Cluster.connect(connectionString, username, password);

    Now that you have a Cluster, add this code snippet to access your Bucket:

    // Get a bucket reference
    Bucket bucket = cluster.bucket(bucketName);
    bucket.waitUntilReady(Duration.ofSeconds(10));

    Add and Retrieve Documents

    The Java SDK supports full integration with the Collections feature introduced in Couchbase Server 7.0. Collections allow documents to be grouped by purpose or theme, according to a specified Scope.

    Here we refer to the users collection within the tenant_agent_00 scope from the Travel Sample bucket as an example, but you may replace this with your own data.

    // Get a user-defined collection reference
    Scope scope = bucket.scope("tenant_agent_00");
    Collection collection = scope.collection("users");

    Data operations, like storing and retrieving documents, can be done using simple methods on the Collection class such as Collection.get() and Collection.upsert().

    Add the following code to create a new document and retrieve it:

    // Upsert Document
    MutationResult upsertResult = collection.upsert(
        "my-document",
        JsonObject.create().put("name", "mike")
    );
    
    // Get Document
    GetResult getResult = collection.get("my-document");
    String name = getResult.contentAsObject().getString("name");
    System.out.println(name); // name == "mike"

    SQL++ Lookup

    Couchbase SQL++ queries can be performed at the Cluster or Scope level by invoking Cluster.query() or Scope.query().

    Cluster level queries require you to specify the fully qualified keyspace each time (e.g. travel-sample.inventory.airline). However, with a Scope level query you only need to specify the Collection name — which in this case is airline:

    // Call the query() method on the scope object and store the result.
    Scope inventoryScope = bucket.scope("inventory");
    QueryResult result = inventoryScope.query("SELECT * FROM airline WHERE id = 10;");
    
    // Return the result rows with the rowsAsObject() method and print to the terminal.
    System.out.println(result.rowsAsObject());

    You can learn more about SQL++ queries on the Query page.

    Execute!

    Now that you’ve completed all the steps, run the example via your IDE or through the command line. You should expect to see the following output:

    mike
    [{"airline":{"country":"United States","iata":"Q5","name":"40-Mile Air","callsign":"MILE-AIR","icao":"MLA","id":10,"type":"airline"}}]

    Next Steps

    Now you’re up and running, try one of the following:

    Additional Resources

    The API reference is generated for each release and the latest can be found here. Older API references are linked from their respective sections in the Release Notes.

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

    If you are planning to use Spring Data Couchbase, see the notes on version compatibility.

    Troubleshooting