22 May 2014

Export/Import MongoDB database collection

Exporting database collection
$ mongoexport -o /var/tmp/collectioname.dump.json -d databasename -c collectioname
This will export the collection to file collectioname.dump.json

Importing database collection
$mongoimport -d databasename -c collectioname --file /var/tmp/collectioname.dump.json

Issue: When we export the collection using  mongoexport it will export the Datatype information along with the value e.g. if Date is stored in collection then the exported file will show the Date field like

"created_at" : { "$date" : "2014-05-19T20:58:22.000+0530" } instead of "created_at" :  "Date(2014-05-19T20:58:22.000+0530)" and similarly of long numbers.

If we import such exported dump file we will get error. To overcome this problem we have to write our custom script to do the conversion.
 
Sample java code for conversion and insertion in MongoDB :

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.util.JSON;

public class TestMongoDBImport {
  
    private static final String DATABASE   = "databasename";
    private static final String COLLECTION      = "collectioname";
    private MongoClient     client     = null;
    private DB          db     = null;
    private DBCollection    collection = null;

    public TestMongoDBImport() throws UnknownHostException {
        this.client = new MongoClient("localhost", 27017);
        this.db = this.client.getDB(DATABASE);
    }

    private void createTable() {
        if (!db.collectionExists(COLLECTION)) {
            BasicDBObject obj = new BasicDBObject();
            obj.append("capped", false);
            db.createCollection(COLLECTION, obj);
        }
        this.collection = db.getCollection(COLLECTION);
    }

    private void insert(ArrayList obj) {
        this.collection.insert(obj);
    }

    public static void main(String[] args) throws IOException {
        String filename = "/var/tmp/collectioname.dump.json";
        TestMongoDBImport tw = new TestMongoDBImport();
        tw.createTable();
        BufferedReader br = new BufferedReader(new FileReader(filename));
        String line = null;
        long runTime = 0;
        long startTime = (new Date()).getTime();
        int i = 0;
        try {
                StringBuilder sb = new StringBuilder();
                line = br.readLine();
                ArrayList recs = new ArrayList();
                while ((line = br.readLine()) != null) {
                    line = line.replaceAll(line = line.replaceAll("\\{ \"\\$date\" : \"([0-9]{4}\\-[0-9]{2}\\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}\\+[0-9]{4})\" \\},",
            "\"Date($1)\","););
                    line = line.replaceAll("\\{ \"\\$numberLong\" : \"(\\d*)\" \\}", "$1");
                    recs.add((DBObject) JSON.parse(line));
                    i++;
                    if (i % 500 == 0) {
                        tw.insert(recs);
                         recs = new ArrayList();
                        System.out.println(i + ":: Added Till");
                    }
                }
                if (recs != null && recs.size() > 0) {
                    tw.insert(recs);
                }
        } finally {
            br.close();
            long endTime = (new Date()).getTime();
            runTime = endTime - startTime;
            System.out.println("Time to upload records " + i + ":"
                + (runTime / 1000) + " seconds");
        }
    }
}





No comments:

Post a Comment