Migrating Couchbase to MongoDB: Adding Documents to Couchbase

The com.couchbase.client.java.CouchbaseCluster class is the client class for Couchbase Server and is the entry point to access Couchbase cluster, which may consist of one or more servers. In the CreateCouchbaseDocument application we shall use the CouchbaseCluster class to create and store a JSON document in Couchbase Server. The CouchbaseCluster class provides the overloaded create() methods to create an instance of CouchbaseCluster. We shall use the static method create() that does not take any args and is used to connect to the default bucket at localhost at port 8091. Couchbase Server stores documents in Data Buckets. The Bucket interface represents a connection to a bucket to perform operations on the bucket synchronously.

  1. Create a CouchbaseCluster instance and subsequently connect to the default bucket using the openBucket() method. For connecting to the “default” bucket, bucket name and password are not required to be specified.

Cluster cluster = CouchbaseCluster.create();

Bucket defaultBucket = cluster.openBucket();

The com.couchbase.client.java.document.json.JsonObject class represents a JSON object stored in Couchbase Server. A document is represented with the com.couchbase.client.java.document.Document interface and several class implementations are provided, including the com.couchbase.client.java. document.JsonDocument, which creates a document from a com.couchbase.client.java.document.json. JsonObject. JsonObject represents a JSON object, the {a1:v1,a2:v2} JSON stored in Couchbase.

The JsonObject class provides static methods empty() and create() to create an empty JsonObject instance. The JsonObject class provides the overloaded put methods to put field/value pairs in a JsonObject instance. The field name in each of these methods is of type String. A put method is provided for each of the value types String, int, long, double, boolean, JsonObject, JsonArray, and Object. We shall make use of the put(String,String) method to add key/value pairs to a JSON document.

  1. Create a JsonObject instance for a JSON document with fields journal, publisher, edition, title, and author with String values using the put(java.lang.String name, java.lang.String value) method. First, invoke the empty() method to return an empty JsonObject instance and subsequently invoke the put(java.lang.String name, java.lang.String value) method to add field/value pairs.

JsonObject catalogObj = JsonObject.empty()

.put(“journal”, “Oracle Magazine”)

.put(“publisher”, “Oracle Publishing”)

.put(“edition”, “March April 2013”)

.put(“title”, “Engineering as a Service”)

.put(“author”, “David A. Kelly”);

  1. The Bucket class provides several overloaded insert and upsert methods to add a document to a bucket. Create an instance of JsonDocument using the JsonDocument. create(java.lang.String id, JsonObject content)method with document id as “catalog.” Use the insert(D document) class to add a JsonObject instance to the default bucket.

defaultBucket.insert(JsonDocument.create(“catalog1”, catalogObj));

  1. Similarly, add another JSON document to the default bucket.

catalogObj = JsonObject.empty()

.put(“journal”, “Oracle Magazine”)

.put(“publisher”, “Oracle Publishing”)

.put(“edition”, “March April 2013”)

.put(“title”, “Quintessential and Collaborative”)

.put(“author”, “Tom Haunert”);

defaultBucket.insert(JsonDocument.create(“catalog2”, catalogObj));

  1. After adding documents disconnect from the Couchbase cluster using the disconnect() method.

cluster.disconnect();

The CreateCouchbaseDocument class is listed below.

package mongodb;

import com.couchbase.client.java.Bucket;

import com.couchbase.client.java.Cluster;

import com.couchbase.client.java.CouchbaseCluster;

import com.couchbase.client.java.document.JsonDocument;

import com.couchbase.client.java.document.json.JsonObject;

public class CreateCouchbaseDocument {

public static void main(String args[]) {

Cluster cluster = CouchbaseCluster.create();

Bucket defaultBucket = cluster.openBucket();

JsonObject catalogObj = JsonObject.empty()

.put(“journal”, “Oracle Magazine”)

.put(“publisher”, “Oracle Publishing”)

.put(“edition”, “March April 2013”)

.put(“title”, “Engineering as a Service”)

.put(“author”, “David A. Kelly”);

defaultBucket.insert(JsonDocument.create(“catalog1”, catalogObj));

catalogObj = JsonObject.empty()

.put(“journal”, “Oracle Magazine”)

.put(“publisher”, “Oracle Publishing”)

.put(“edition”, “March April 2013”)

.put(“title”, “Quintessential and Collaborative”)

.put(“author”, “Tom Haunert”);

defaultBucket.insert(JsonDocument.create(“catalog2”, catalogObj));

cluster.disconnect();

}

}

  1. To run the CreateCouchbaseDocument.java application right-click on the class in Package Explorer and select Run As Java Application as shown in Figure 7-10.

The JSON documents get stored in the Couchbase Server. Log in to the Couchbase Server Administration Console if not already logged in. Click on Data Buckets. The Item Count for the “default” bucket should be listed as 2 as shown in Figure 7-11. Click on Documents to list the documents added.

  1. The two documents added get listed as shown in Figure 7-12. Click on Edit Document to display the JSON for a document.

The catalogl ID JSON document gets displayed as shown in Figure 7-13.

8. Similarly list the catalog2 document as shown in Figure 7-14.

Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.

Leave a Reply

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