Getting Started With Couchbase Server on Java SDK

DZone 's Guide to

Getting Started With Couchbase Server on Java SDK

Read this article in order to learn more about how to use the Couchbase Server with the Java SDK.

· Database Zone ·
Free Resource

In this article, we will learn how to use the Couchbase Server with the Java SDK.

Couchbase is not new to the market; it has been around since 2012. Couchbase Server is an open-source distributed multi-model NoSQL document-oriented database software package that is optimized for interactive applications. These applications may serve many concurrent users by creating, storing, retrieving, aggregating, manipulating, and presenting data. In support of these kinds of application needs, Couchbase Server is designed to provide easy-to-scale key-value or JSON document access with low latency and high sustained throughput. It is designed to be clustered from a single machine to very large-scale deployments spanning many machines.

As we proceed, we will see how to interact with Couchbase document database, create an environment, cluster and other stuff required to run our queries on the database.

Before we start, you can download Couchbase Server by clicking: Download Here.

After you download the server.deb file, follow these steps to get your server up and running:

Installing "couchbaseServer.deb" file on Linux.

Step1: Open up a terminal/commandPrompt and move to the folder containig your downloaded file.

Step2: Check if you have openssl installed on your system issue the following command : openssl version if it is not installed then install it first.

Step3: Run sudo dpkg -i 

Step3 will start couchbase as a service on your system to check if service is installed and working issue the following command: 
- sudo service couchbase-server status
you will see following output:

● couchbase-server.service - Couchbase Server
 Loaded: loaded (/lib/systemd/system/couchbase-server.service; enabled; vendor
 Active: active (running) since Sun 2018-04-08 19:45:28 IST; 2s ago
 Docs: http://docs.couchbase.com

Now that you have installed the couchbase server, you can look at its UI here localhost:8091. Once you provide the username and password to your server, you are good to go start creating your buckets on the server.

So, let's get started.

Step 1 

I'll be using a Maven project here, and we will need to add the following dependency to our pom.xml to integrate Couchbase with our application:


Step 2 

Start with the Default Environment provide by the Couchbase, or go ahead and create your own custom Environment. But be cautious, Couchbase community advises the users to ensure that only one CouchbaseEnvironment is active in the JVM since the use of two or more environments may lead to an unpredictable behavior. If you don't need any fine-tuning of the basic settings, just use the DefaultCouchbaseEnvironment and associate that to your cluster by simply issuing the following:

Cluster cluster = CouchbaseCluster.create("");// here we connect to a single node cluster on our localhost

To connect to a multi-node cluster, we need to specify at least two nodes in case one of them is unavailable when the application attempts to establish the connection, as shown:

Cluster cluster = CouchbaseCluster.create("","");//there can be other nodes as well in the cluster but we don't need to pass them all here since the couchbase environment would later on query the cluster on connection establishment to discover the other nodes if present. 

In case you want to create your custom environment, use the following configurations, and then connect to the cluster using this environment:

CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()

Cluster cluster= CouchbaseCluster.create( env , "");

Step 3

After establishing the connection to the cluster, the next step is to create a bucket or use an existing bucket. A bucket is your key-value store wherein you store your documents indexed with a unique ID for fast transactions. To open an existing bucket, you just need to know the bucket name (a password protected bucket is also available that requires both the bucket name and password to be passed to open the bucket) and issue the following segment:

Bucket bucket = cluster.openBucket("bucket-name");

In case we want to create our own bucket with some custom settings, we can do so by:

//Create your bucket.....
BucketSettings sampleBucket = new DefaultBucketSettings.Builder()
.quota(200) // megabytes

Then, we just need to add our newly created bucket to the cluster we have formed. This could be done with the help of the clusterManager method of Cluster class, as shown below:

cluster.clusterManager("couchbase.cluster.username", "couchbase.cluster.password")

The above statement will insert our sampleBucket into the cluster.

Step 4 

Now that the Bucket has been inserted, we need to store the documents in the bucket and later we will query directly on those documents. For querying purposes, Couchbase uses a query language called the non-first normal form query language, N1QL (pronounced nickel) is used for manipulating the JSON data in Couchbase just like SQL manipulates data in RDBMS. It has SELECT, INSERT, UPDATE, DELETE, and MERGE statements to operate on JSON data.


  "email": "anyMail@gmail.com",
  "address": [

Like Query:

SELECT * FROM `sampleBucket` WHERE email LIKE "%@gmail.com";
Array Query:
SELECT * FROM `sampleBucket` WHERE ANY person IN address SATISFIES person.city = "DEF" END;

I hope that this information helps you when you're just starting off with Couchbase server. For more detailed programming logic, you can clone the repository that I have setup for my Couchbase implementation.

Clone Me

In case you run into any trouble while dealing with Couchbase, you can always get in touch with me, and I will try to resolve your doubts and issues ASAP. Please feel free to comment your doubts or anything else that you would like to add or want me to introduce in my post.

Thanks for the support.

References: https://developer.couchbase.com/documentation/server/5.1/introduction/intro.html

couchbase ,couchbase server ,database ,java sdk

Published at DZone with permission of Prashant Sharma , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}