Wednesday, June 25, 2014

java based MonogoDB application on BlueMix

NoSQL databases are finding significant and growing industry use in big data and real-time web applications,it provide way to save unstructured data.
Use Cases of ever increasing unstructured data
    Facebook:
        750 million users are active , 1 in 3 Internet users have a Facebook account
        More than 30 billion pieces of content (web links, news stories, blog posts, notes, photo albums, etc.) shared each month.
        Holds 30PB of data for analysis, adds 12 TB of compressed data daily
    Twitter
        200 million users, 200 million daily tweets
        1.6 billion search queries a day
        7 TB data for analysis generated daily

Here is a simple example showing how to save data to mongodb from java web application on BlueMix

1.Get the code from https://hub.jazz.net/project/communitysample/mongodb-java/overview
2. Unzip the code to your driver

3.If you have install maven,use mvn package to generate the war file.


4.Switch the dos command and use below command to push you war to the bluemix.

   cf p javamongodb  -p java-mongodb-sample.war
5.Create your mongodb service
cf create-service mongodb 100 mongodbLinedIN

6.Bind your mongodb service

cf bind-service javamongodb mongodbLinedIN

7.use cf push to make the env effect.

8.Launch the url of the app,you will see the data has been inserted into Mongodb.

Detail code: MainServlet ,we need to add mogodb ,cloudfoundry lib on the classpath

package com.ibm.bluemix.mongodb;

import java.util.*;

import org.cloudfoundry.runtime.env.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import com.mongodb.*;

public class MainServlet extends HttpServlet implements Servlet {


  protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    try{
        String connURL = getServiceURI();
        MongoClient mongo = new MongoClient(new MongoClientURI(connURL));

        DB db = mongo.getDB("db");

        DBCollection table = db.getCollection("user");


        BasicDBObject document = new BasicDBObject();
        document.put("name", "Tom");
        document.put("age", 30);
        document.put("createdDate", new Date());
        table.insert(document);

        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("name", "Tom");

        DBCursor cursor = table.find(searchQuery);

        while (cursor.hasNext()) {
            out.println( "Inserted: " + cursor.next());
        }

        BasicDBObject query = new BasicDBObject();
        query.put("name", "Tom");

        BasicDBObject newDocument = new BasicDBObject();
        newDocument.put("name", "Tina");

        BasicDBObject updateObj = new BasicDBObject();
        updateObj.put("$set", newDocument);

        table.update(query, updateObj);

        BasicDBObject searchQuery2 = new BasicDBObject().append("name", "Tina");

        DBCursor cursor2 = table.find(searchQuery2);

        while (cursor2.hasNext()) {
            out.println( "Updated: " + cursor2.next());
        }

        out.println("Success!!");

    } catch (Exception e) {
        out.println("Failed: " + e.getMessage());
        e.printStackTrace();
    }
  }

  public String getServiceURI() throws Exception {
    CloudEnvironment environment = new CloudEnvironment();
    if ( environment.getServiceDataByLabels("mongodb").size() == 0 ) {
        throw new Exception( "No MongoDB service is bund to this app!!" );
    }

    Map credential = (Map)((Map)environment.getServiceDataByLabels("mongodb").get(0)).get( "credentials" );

    return (String)credential.get( "url" );
  }

}

2 comments:

  1. The code in this post is outdated. The service label has changed from "mongodb" to "mongodb-2.4". The code in there referenced project at https://hub.jazz.net/project/communitysample/mongodb-java/overview appears to be correct.

    ReplyDelete