Wednesday, May 23, 2018

Establishing Connection to CouchBase Database

Connecting to Couch

All the dependencies are added, Now lets start coding.
I'm going to use "Spring Boot", Since it is very quick and easy way to start with.

Connecting to a Cluster with a Default Environment

A cluster is a collection of one or more instances of Couchbase Server that are configured as a logical cluster.
All nodes within the cluster are identical and provide the same functionality. Create a Couchbase Environment with default settings and associate it with our cluster.

we can connect to the cluster simply by providing the IP address or hostname of one or more nodes in the cluster. In this example, we connect to a single-node cluster on our local workstation.
   
 Cluster cluster = CouchbaseCluster.create("localhost");
    
To connect to a multi-node cluster, we would specify at least two nodes in case one of them is unavailable when the application attempts to establish the connection.
     
 Cluster cluster = CouchbaseCluster.create("192.168.4.1", "192.168.4.2");
    

Opening a Bucket

Once you have connected to the Couchbase cluster, you can open one or more buckets.
     
Bucket bucket = cluster.openBucket("BucketName","Password");

//if no password is set
Bucket bucket = cluster.openBucket("BucketName");
    

Document IDs

Each document stored in Couchbase is associated with an id that is unique to the bucket in which the document is being stored.
The document id is analogous to the primary key column in a traditional relational database row.

In Couchbase you have to generate the unique document ID to save the document,It is not the case in mongo,
morphia will generate it for you but the best practice is to always find a unique primary in your entity and set it as document ID.
Share:

0 comments:

Post a Comment