softwaremom

Just another WordPress.com site

MongoDB Driver Import Issue – Resolved

Tonight my younger daughter had a orchestra concert at her school.  I went and sat on the gym bench reading “Hadoop: The Definitive Guide”.  Even though I cannot do some hands-on exercises, it gets me started to see the actual Map/Reduce Java code.

After the concert I continue to work on my first Java MongoDB program.  Since the issue was that the interpreter cannot find the MongoDB Java driver, I re-downloaded mongo-java-driver-2.11.3.jar file and place it in the /usr/lib/mongodb folder (created by me).  I am not sure if it’s the right folder but I gave it a try.  Some blogger suggested to put it in the JAVA_HOME folder.  That led me to discover that I didn’t have the variable set.  After some searching and I’ve found that I have multiple versions (6 and 7) of openjre jar files in /user/lib/jvm  folder.  So, I set that variable in my .bashrc file for future use.

I’ve found in the forum (http://forums.devshed.com/java-help-9/importing-a-jar-file-with-eclipse-595444.html), someone has a similar question and this is the answer that worked:

In project explorer right click on the folder/package where you want to import the JAR. Then choose “Import”. In the import wizard, select General > File system. Browse for the JAR and select it.Now you imported the JAR, but before you can use it, you need to add it to the classpath. Once again in project explorer right click on the project root folder, select Properties. In the properties menu select Java Build Path. In Java Build Path open the libraries tab and click on “Add JARs…” and browse for the JAR you just imported.

If you do not wish to actually import the package to your project but just use it, then you can skip the first part of this instruction and directly go to the “Libraries” tab in the Java Build Path. This time you should click on “Add external JARs…” and browse for the JAR in your file system.

I imported the jar file by selecting the Import option after right click on the project icom in Package Explorer, then select General/File system and browse to the folder.  After the selection, the list boxes on the right showed the jar files but I check the folder on the left list box.  I guess it means importing all the jar files in that folder.  

I didn’t set classpath but it worked.  I’ve added some code to add documents to the collection and iterate through the documents in the collection to print out the objects.

I like how the driver support  DBObject that serializing into JSON format.

 

BasicDBObject doc = new BasicDBObject("name", "MongoDB").
                              append("type", "database").
                              append("count", 1).
                              append("info", new BasicDBObject("x", 203).append("y", 102));

coll.insert(doc);

    for (DBObject obj : col.find()) {
    System.out.println(obj.toString());

The result is:

{ “_id” : { “$oid” : “52b146b8e4b04530a89967c2”} , “name” : “John” , “type” : “Singer” , “lastname” : “Lennon” , “info” : { “x” : 203 , “y” : 102}}

I guess they might have the same thing for C# and I just haven’t came across it.  So far I’ve only tried the Linq driver, which does require you to define a POCO class to represent it’s schema.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: