If it helps someone.

Menu

Export data from MySQL to Mongo DB

This is a continuation of my previous article Moving data between HBase and MySQL. In this article we are adding one more database, which is mongoDB. I will be using the same base framework and interfaces. The example shows how to export mysql to mongodb.

1. Introduction

mongoDB is a leading noSQL document database. It stores data in document format. If you are familiar with JSON structure, then you will understand exactly how data is represented in mongoDB. For example

{
id : "1234",
name :
{
first_name : "ABC",
last_name : "xyz",
}
}

mongoDB uses a type of JSON called BSON. There is no concept of tables in mongoDB. Instead it uses collections. In mongoDB, you need not create objects like collections, db, explicitely. First usage of the same creates it. For installation details please refer getting started from mongoDB.

For using examples, from this article, you need java driver for mongoDB. This can be donloaded from here .

2. Settig up to mongoDB reader and writer

I will use the same framework mentioned in my earlier post. The constructor will take the name of collection and the columns we are looking for.

Note: This is an old article. Copied here when moving to new hosting provider.

This sample will not use any nested values. But that does not stop you from using one.

3. Connecting to mongoDB

The connect() method implemented by MongoDBSource will create an instance of MongoClient. This is enough for connecting to mongoDB instance. As we are using one class instance for a collection, lets create the collection instance also and save it.

4. Reading records

Reading from collection is as simple as processing an iterator. As we are not using any filters, we call the find() API of DBCollection instance. This will return DBCursor instance to iterate through records. Every record is a DBObject instance from which column values can be accessed like a map.