//

MongoDB: Supplemental – GRIDFS Example in Java

13.11.2012 | 4 minutes of reading time

As mentioned earlier it is time to do some real stuff in this blogging series. Therefore it is good that this week’s class had an example for storing blobs in MongoDB using Python. Yes, well, Python, you know, that is the programming language with these very strange indentation rules. Thus I thought to myself: Let’s do something really cool and fancy! But I had no idea what thus I ended up doing this in Java ;-).

The internet is full to the brim with examples and tutorials on how to do some Java programming in MongoDB. But I thought it will not hurt if I add another small example on top. The first thing to recommend is the Java Tutorial on the MongoDB homepage . I found it to be an excellent starting point. Looking for the Java libraries for using MongoDB? The search is over!

Of course there is an API documentation available for the Java library and there is also more information on GRIDFS in the corresponding specification . But why bothering reading through all these documents. Let’s jump to some Java example right away.

1package de.mongodb.gridfs.sample;
2 
3import java.io.File;
4import java.io.IOException;
5 
6import com.mongodb.BasicDBObject;
7import com.mongodb.DB;
8import com.mongodb.DBCollection;
9import com.mongodb.Mongo;
10import com.mongodb.WriteConcern;
11import com.mongodb.gridfs.GridFS;
12import com.mongodb.gridfs.GridFSInputFile;
13 
14public class GRIDFSSample {
15 
16 
17    public static void main(String[] args) throws IOException {
18 
19        //
20        // Connect to MongoDB (without authentification for the time being)
21        // And get a handle on the collection used to store the metadata
22        //
23        Mongo mongo = new Mongo("localhost", 27017);
24        DB db = mongo.getDB("test");
25        DBCollection collection = db.getCollection("downloads_meta");
26 
27        //
28        // The biiiiig file to be stored to MongoDB
29        //
30        File file = new File("/Users/thomasjaspers/Downloads/mongodb-osx-x86_64-2-1.2.1.tgz");
31 
32        //
33        // Store the file to MongoDB using GRIDFS
34        //
35        GridFS gridfs = new GridFS(db, "downloads");
36        GridFSInputFile gfsFile = gridfs.createFile(file);
37        gfsFile.setFilename("MongoDB-OSX-2-1.2.1");
38        gfsFile.save();
39 
40        //
41        // Let's create a new JSON document with some "metadata" information on the download
42        //
43        BasicDBObject info = new BasicDBObject();
44                info.put("name", "MongoDB");
45                info.put("fileName", "MongoDB-OSX-2-1.2.1");
46                info.put("rawName", "mongodb-osx-x86_64-2-1.2.1.tgz");
47                info.put("rawPath", "/Users/thomasjaspers/Downloads/");
48 
49                //
50                // Let's store our document to MongoDB
51                //
52        collection.insert(info, WriteConcern.SAFE);
53    }	
54}
55

Hopefully the example is pretty straightforward and good to understand on its own. Basically there are two things happening there. The blob is stored to MongoDB using GRIDFS. Afterwards a document is created to MongoDB (to a different collection) using the Java-version of the commands we have seen in the course of this blog series. For the sake of keeping the example short absolutely no error handling is done, but any exception will be simple thrown to the command line.

Let’s take a look what we have in MongoDB after running the Java program. Therefore I display the collections that are available inside my test-db in the Mongo-shell.


> show collections
candidates
downloads.chunks
downloads.files
downloads_meta
instructors
names
ships
startrek
system.indexes

If you followed this blog series you might recognise some of the collections from earlier examples. Especially the ones referring to Star Trek. Just starting to wonder if MongoDB could handle some megaquad of information. Would be handy storing my EMH to MongoDB. Ok, probably that is a different story. Back to the collections and there we can see a few ones that look familiar comparing them to the Java implementation:

  • downloads.chunksMongoDB has created this one to store smaller chunks of the big files. This way it can stay inside the limits of a maximum size of 16 MB it has for one document.
  • downloads.files – This collection contains the document of the actual file, so that we do not have to fiddle around with individual chunks on our own.
  • downloads_meta – This one has been created automatically when we saved the metadata document inside the Java program.

Checking the files-document a lot of – what I would say is – MongoDB internal attributes can be seen, but also the filename that we have given for this file in the Java program. I am not going to look into individual chunks, but let’s have a count to see how many have been created for this document, which by the way had a size of roughly 174,4 MB.


> db.downloads.files.find().pretty()
{
    "_id" : ObjectId("509ec4dea0eebce38606d798"),
    "chunkSize" : NumberLong(262144),
    "length" : NumberLong(178421760),
    "md5" : "1522b62e75c973b7b1d780695807047c",
    "filename" : "MongoDB-OSX-2-1.2.1",
    "contentType" : null,
    "uploadDate" : ISODate("2012-11-10T21:19:26.256Z"),
    "aliases" : null
}
> db.downloads.chunks.count()
681

And finally a look at the metadata that we have created altogether with storing the file. No surprises here I would say.


> db.downloads_meta.find().pretty()
{
    "_id" : ObjectId("509ec4e2a0eebce38606da42"),
    "name" : "MongoDB",
    "fileName" : "MongoDB-OSX-2-1.2.1",
    "rawName" : "mongodb-osx-x86_64-2-1.2.1.tgz",
    "rawPath" : "/Users/thomasjaspers/Downloads/"
}

Ok, this was meant to get a very rough first impression on how it would feel to do some Java programming on top of MongoDB. Feels good :-)! I found it really easy to start with, which is a thing I would not say if someone would start using for example JPA on top of some relational database. Of course more hacking is required for a final judgement. I have already a small project in mind …


The MongoDB class series

Part 1 – MongoDB: First Contact
Part 2 – MongoDB: Second Round
Part 3 – MongoDB: Close Encounters of the Third Kind
Part 4 – MongoDB: I am Number Four
Part 5 – MongoDB: The Fith Element
Part 6 – MongoDB: The Sixth Sense
Part 7 – MongoDB: Tutorial Overview and Ref-Card

Java Supplemental Series

Part 1 – MongoDB: Supplemental – GRIDFS Example in Java
Part 2 – MongoDB: Supplemental – A complete Java Project – Part 1
Part 3 – MongoDB: Supplemental – A complete Java Project – Part 2

share post

Likes

0

//

More articles in this subject area\n

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen

Wir helfen Deinem Unternehmen

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.