rokpoto.com
rokpoto.com

Reputation: 10794

Fastest way to import millions of JSON documents to MongoDB

I have more than 10 million JSON documents of the form :

["key": "val2", "key1" : "val", "{\"key\":\"val", \"key2\":\"val2"}"]

in one file.

Importing using JAVA Driver API took around 3 hours, while using the following function (importing one BSON at a time):

public static void importJSONFileToDBUsingJavaDriver(String pathToFile, DB db, String collectionName) {
    // open file
    FileInputStream fstream = null;
    try {
        fstream = new FileInputStream(pathToFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.println("file not exist, exiting");
        return;
    }
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

    // read it line by line
    String strLine;
    DBCollection newColl =   db.getCollection(collectionName);
    try {
        while ((strLine = br.readLine()) != null) {
            // convert line by line to BSON
            DBObject bson = (DBObject) JSON.parse(JSONstr);
            // insert BSONs to database
            try {
                newColl.insert(bson);
            }
            catch (MongoException e) {
              // duplicate key
              e.printStackTrace();
            }


        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }


}

Is there a faster way? Maybe, MongoDB settings may influence the insertion speed? (for, example adding key : "_id" which will function as index, so that MongoDB would not have to create artificial key and thus index for each document) or disable index creation at all at insertion. Thanks.

Upvotes: 13

Views: 21114

Answers (7)

Sam Wolfand
Sam Wolfand

Reputation: 54

You can use a bulk insertion

You can read the documentation at mongo website and you can also check this java example on StackOverflow

Upvotes: 0

PUG
PUG

Reputation: 4472

Use bulk operations insert/upserts. After Mongo 2.6 you can do Bulk Updates/Upserts. Example below does bulk update using c# driver.

MongoCollection<foo> collection = database.GetCollection<foo>(collectionName);
      var bulk = collection.InitializeUnorderedBulkOperation();
      foreach (FooDoc fooDoc in fooDocsList)
      {
        var update = new UpdateDocument { {fooDoc.ToBsonDocument() } };
        bulk.Find(Query.EQ("_id", fooDoc.Id)).Upsert().UpdateOne(update);
      }
      BulkWriteResult bwr =  bulk.Execute();

Upvotes: 2

Bruno D. Rodrigues
Bruno D. Rodrigues

Reputation: 400

I'm sorry but you're all picking minor performance issues instead of the core one. Separating the logic from reading the file and inserting is a small gain. Loading the file in binary mode (via MMAP) is a small gain. Using mongo's bulk inserts is a big gain, but still no dice.

The whole performance bottleneck is the BSON bson = JSON.parse(line). Or in other words, the problem with the Java drivers is that they need a conversion from json to bson, and this code seems to be awfully slow or badly implemented. A full JSON (encode+decode) via JSON-simple or specially via JSON-smart is 100 times faster than the JSON.parse() command.

I know Stack Overflow is telling me right above this box that I should be answering the answer, which I'm not, but rest assured that I'm still looking for an answer for this problem. I can't believe all the talk about Mongo's performance and then this simple example code fails so miserably.

Upvotes: 10

Yadli
Yadli

Reputation: 381

I've done importing a multi-line json file with ~250M records. I just use mongoimport < data.txt and it took 10 hours. Compared to your 10M vs. 3 hours I think this is considerably faster.

Also from my experience writing your own multi-threaded parser would speed things up drastically. The procedure is simple:

  1. Open the file as BINARY (not TEXT!)
  2. Set markers(offsets) evenly across the file. The count of markers depends on the number of threads you want.
  3. Search for '\n' near the markers, calibrate the markers so they are aligned to lines.
  4. Parse each chunk with a thread.

A reminder:

when you want performance, don't use stream reader or any built-in line-based read methods. They are slow. Just use binary buffer and search for '\n' to identify a line, and (most preferably) do in-place parsing in the buffer without creating a string. Otherwise the garbage collector won't be so happy with this.

Upvotes: 5

Jhanvi
Jhanvi

Reputation: 5149

You can parse the entire file together at once and the insert the whole json in mongo document, Avoid multiple loops, You need to separate the logic as follows:

1)Parse the file and retrieve the json Object.

2)Once the parsing is over, save the json Object in the Mongo Document.

Upvotes: 4

evanchooly
evanchooly

Reputation: 6243

You can also remove all the indexes (except for the PK index, of course) and rebuild them after the import.

Upvotes: 2

tom
tom

Reputation: 2712

I've got a slightly faster way (I'm also inserting millions at the moment), insert collections instead of single documents with

insert(List<DBObject> list)

http://api.mongodb.org/java/current/com/mongodb/DBCollection.html#insert(java.util.List)

That said, it's not that much faster. I'm about to experiment with setting other WriteConcerns than ACKNOWLEDGED (mainly UNACKNOWLEDGED) to see if I can speed it up faster. See http://docs.mongodb.org/manual/core/write-concern/ for info

Another way to improve performance, is to create indexes after bulk inserting. However, this is rarely an option except for one off jobs.

Apologies if this is slightly wooly sounding, I'm still testing things myself. Good question.

Upvotes: 3

Related Questions