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.x is a complete rewrite of the 2.x API, providing a simpler surface area and adding support for Couchbase Server features like Collections and Scopes (available in Couchbase Server 7.0).
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.
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. The latest version (as of July 2021) is 3.2.0.
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.2.0</version>
</dependency>
</dependencies>For Gradle, you can use:
implementation 'com.couchbase.client:java-client:3.2.0'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.
Once you have the Java client installed, open your IDE, and try out the following:
link:example$StartUsing.java[role=include]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:
link:example$StartUsing.java[role=include]To access the KV (Key/Value) API or to query views, you need to open a Bucket:
link:example$StartUsing.java[role=include]If you installed the travel-sample data bucket, substitute travel-sample for bucket-name.
The 3.2 SDK supports full integration with the Collections feature in the latest release of the Couchbase Data Platform, Couchbase Server 7.0.
This brings complete support of Collections, allowing Documents to be grouped by purpose or theme, according to a specified Scope.
Here we will use the users collection within the tenant_agent_00 scope from travel-sample bucket as an example.
link:example$StartUsing.java[role=include]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.
link:example$StartUsing.java[role=include]You can also perform a N1QL query at the cluster level:
link:example$StartUsing.java[role=include]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.
If you want to copy and paste to run the full example, here it is:
- Couchbase Playground
-
Run this example right here in your browser.
link:example$StartUsing.java[role=include]
- Local Couchbase Server
-
Copy and run this example into your local environment. Note that Couchbase Server and SDK must be installed and running before executing this example.
link:example$StartUsing.java[role=include]
- Couchbase Cloud
-
To connect to Couchbase Cloud, replace the variables in this example with the correct endpoint as well as user, password, and
couchbasecloudbucketfor your account. Also see the Cloud section below for additional details.link:devguide:example$CloudConnect.java[role=include]
For developing on Couchbase Cloud, if you are not working from the same Availability Zone, refer to the following:
-
Notes on Constrained Network Environments,
-
Network Requirements,
-
If you have a consumer-grade router which has problems with DNS-SRV records review our Troubleshooting Guide.
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.