Migrating Couchbase to MongoDB: Migrating Couchbase Documents to MongoDB

In this section we shall query the JSON documents stored earlier in Couchbase Server and migrate the JSON to MongoDB database. We shall use the MigrateCouchbaseToMongoDB application to migrate the JSON documents from Couchbase Server to a MongoDB database. We added a view encapsulated in a design document to Couchbase Server so that we may use the view to query the Couchbase Server. A view is represented with the com.couchbase.client.java.view.View class, and a view query is represented with the com.couchbase.client.java.view.ViewQuery class, which provides the from(java.lang.String design, java.lang.String view) class method to create a ViewQuery instance. The Bucket class provides the overloaded query() method to query a view. Each of the query() methods return a ViewResult instance, which represents the result from a ViewQuery. We shall generate a ViewResult for the documents stored in Couchbase Server using a view query and subsequently iterate over the view result to migrate the JSON documents to MongoDB.

  1. In the MigrateCouchbaseToMongoDB application’s main method create an instance of Bucket as discussed earlier.

Cluster cluster = CouchbaseCluster.create();

Bucket defaultBucket = cluster.openBucket();

  1. Also as discussed in Chapter 1, create an instance of MongoCollection for the catalog collection to which the Couchbase documents are to be migrated.

mongoClient = new MongoClient(Arrays.asList(new ServerAddress(“localhost”, 27017)));

MongoDatabase db = mongoClient.getDatabase(“local”);

MongoCollection<Document> coll = db.getCollection(“catalog”);

  1. Having created a connection with the Couchbase Server and the MongoDB server we shall migrate the Couchbase documents to MongoDB. Invoke the query(ViewQuery query) method using the Bucket instance to generate a ViewResult object. Create a ViewQuery argument using the static method from(java.lang.String design, java.lang.String view) with the design document name as catalog and view name as catalog_view, which were created in the preceding section.

ViewResult result = defaultBucket.query(ViewQuery.from(“catalog”,”catalog_view”));

  1. ViewResult provides the overloaded rows() method that returns an Iterator over the rows in the view result. The ViewRow interface represents a view row. Using an enhanced for loop, iterate over the rows in the ViewResult and output each row to MongoDB.

for (ViewRow row : result) {

//Migrate each row to MongoDB

}

  1. A document in MongoDB Java driver is represented with the org.bson.Document class. Create an instance of Document for each row in the ViewResult. The JSON document in Couchbase Server driver is represented with the JsonDocument class. A JsonDocument instance may be obtained from a ViewRow instance using the document() method. Subsequently the JSON object is obtained from the JsonDocument with the content() method. The JsonObject instance has field/value pairs for a JSON document. Obtain the field names from the JsonObject as a Set using the getNames() method. Obtain an Iterator from the Set using the iterator() method. Using a while loop iterate over the field names and get each field name as a String. Obtain the field value using the getString(String fieldName) method in JsonObject. Using the append(String key, Object value) method in Document add the field/value pairs to the BSON document to be stored in MongoDB.

for (ViewRow viewRow : result) {

Document catalog = new Document();

JsonDocument json = viewRow.document();

JsonObject jsonObj = json.content();

Set<java.lang.String> fieldNames = jsonObj.getNames();

Iterator<String> iter = fieldNames.iterator();

while (iter.hasNext()) {

String fieldName = iter.next();

String fieldValue = jsonObj.getString(fieldName);

catalog = catalog.append(fieldName, fieldValue);

}

  1. Having created the Document instance to be stored in MongoDB invoke the insertOne(TDocument document) method using the MongoCollection instance to store the BSON document in MongoDB.

coll.insertOne(catalog);

The MigrateCouchbaseToMongoDB application is listed below.

package mongodb;

import java.util.Arrays;

import java.util.Iterator;

import java.util.Set;

import org.bson.Document;

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;

import com.couchbase.client.java.view.ViewQuery;

import com.couchbase.client.java.view.ViewResult;

import com.couchbase.client.java.view.ViewRow;

import com.mongodb.MongoClient;

import com.mongodb.ServerAddress;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoDatabase;

public class MigrateCouchbaseToMongoDB {

private static Bucket defaultBucket;

private static MongoClient mongoClient;

public static void main(String[] args) {

Cluster cluster = CouchbaseCluster.create();

defaultBucket = cluster.openBucket();

migrate();

}

public static void migrate() {

mongoClient = new MongoClient(Arrays.asList(new ServerAddress( “localhost”, 27017)));

MongoDatabase db = mongoClient.getDatabase(“local”);

MongoCollection<Document> coll = db.getCollection(“catalog”);

ViewResult result = defaultBucket.query(ViewQuery.from(“catalog”, “catalog_view”));

for (ViewRow viewRow : result) {

Document catalog = new Document();

JsonDocument json = viewRow.document();

JsonObject jsonObj = json.content();

Set<java.lang.String> fieldNames = jsonObj.getNames();

Iterator<String> iter = fieldNames.iterator();

while (iter.hasNext()) {

String fieldName = iter.next();

String fieldValue = jsonObj.getString(fieldName);

catalog = catalog.append(fieldName, fieldValue);

}

coll.insertOne(catalog);

}

}

}

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

8.The Couchbase Server documents get migrated to MongoDB. Subsequently run the following commands in Mongo shell.

>use local

>db.catalog.find()

The two JSON documents migrated to MongoDB get listed as shown in Figure 7-21.

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 *