Couchbase

Couchbase Server  is an open source NoSQL document based database that is optimized for interactive web applications. It provides programmatic access to Couchbase server through the SDKs of the different programming languages.

It has a query language known as N1QL, which has a structure composed of statements, expressions, and comments.

Installation

To install latest version of Couchbase in Ubuntu 16.04 (xenial):

First, download Couchbase server from here. Make sure you choose ubuntu 16.04 as the operating system.

Then reload the local package database.

Install Couchbase Server using the following command:

where ./ is the path to the Couchbase server package and * is the version of the package that you have downloaded.

If dpkg reports any errors about missing dependencies, run the following command to download and install them:

Once installation is complete, Couchbase Server will start automatically.

Getting started with Couchbase

Open this link to access the Couchbase Server Web Console.

Enter the username and password that you have provided during installation and click sign in.

Cluster Dashboard

After signing in successfully, you will be forwarded to Cluster Dashboard.

The Cluster Dashboard provides a graphical summary of the current state of your Couchbase cluster.

Click on Buckets in the left-hand navigation bar to go to the bucket screen. On the bucket screen, click on “Add Bucket” to add a new bucket.

Give it a name and determine memory quota then click “Add Bucket”

After adding a bucket, go to “Query” screen to create primary index for your bucket. Use the following query:

CREATE PRIMARY INDEX ON `bucket_name` USING GSI;

Replace `bucket_name` with the name that you have given to your bucket. Then Execute.

You have successfully created your first bucket.


Using Java SDK with a java application

First, download Java SDK from here.

Then extract the zip file and add the following jars to your project:

  • couchbase-java-client:2.6.2
  • couchbase-core-io:1.6.2
  • rxjava:1.3.7
  • opentracing-api:0.31.0

The code segment below describes how the Java SDK can be used for some common operations (described in comments):

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

public class Example {
  public static void main(String[] args) throws Exception {

// Initialize the Connection
  Cluster cluster = CouchbaseCluster.create("localhost");
cluster.authenticate("username", "password"); Bucket bucket = cluster.openBucket("bucketname");


// Create a JSON Document JsonObject arthur = JsonObject.create() .put("name", "Arthur") .put("email", "kingarthur@couchbase.com") .put("interests", JsonArray.from("Reading", "Sport"));

// Store the Document bucket.upsert(JsonDocument.create("u:king_arthur", arthur));


// Load the Document and print it
// Prints Content and Metadata of the stored Document
System.out.println(bucket.get("u:king_arthur"));


// Create a N1QL Primary Index (but ignore if it exists) bucket.bucketManager().createN1qlPrimaryIndex(true, false);


// Perform a N1QL Query
N1qlQueryResult result = bucket.query(N1qlQuery.parameterized("SELECT name FROM `bucketname` WHERE $1 IN interests", JsonArray.from("Sport")));

// Print each found Row
for (N1qlQueryRow row : result) {

// Prints {"name":"Arthur"}
System.out.println(row);

   }
  }
}

References

https://docs.couchbase.com/server/current/install/ubuntu-debian-install.html

https://docs.couchbase.com/server/current/manage/manage-buckets/create-bucket.html

https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/createprimaryindex.html

https://docs.couchbase.com/java-sdk/2.7/start-using-sdk.html


Leave a Reply

Your email address will not be published. Required fields are marked *