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

Cluster cluster = Cluster.connect(connectionString, 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(bucketName);

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"); String name = getResult.contentAsObject().getString("name"); System.out.println(name); // name == "mike"

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());