PL I – ASSIGNMENT 6

ASSIGNMENT NO.6

Title:

Use MongoDB to process semi structured and unstructured data collections such as Rfid, images, blogs use python/Java MongoDB interface.

Requirements:

  1. Computer System with Windows/Linux/Open Source Operating System.
  2. JDK
  3. Eclipse / Netbeans (for J2EE)
  4. Web Server (Apache Tomcat /Glassfish)
  5. MongoDB Server and Client

Theory:

MongoDB is a powerful, flexible, and scalable data store. It combines the ability to scale out with many of the most useful features of relational databases, such as secondary indexes, range queries, and sorting. MongoDB is also incredibly featureful: it has tons of useful features such as built-in support for MapReduce-style aggregation and geospatial indexes. MongoDB is a document-oriented database, not a relational one.

Some of the basic concepts of MongoDB:

  • A document is the basic unit of data for MongoDB, roughly equivalent to a row in a relational database management system (but much more expressive).
  • Similarly, a collection can be thought of as the schema-free equivalent of a table.
  • A single instance of MongoDB can host multiple independent databases, each of which can have its own collections and permissions.
  • MongoDB comes with a simple but powerful JavaScript shell, which is useful for the administration of MongoDB instances and data manipulation.
  • Every document has a special key, “_id”, that is unique across the document’s collection.

Connect to database:  To connect database, you need to specify database name, if database doesn’t exist then mongodb creates it automatically.

Code snippets to connect to database would be as follows:

//import required packages

try{

// To connect to mongodb server

MongoClient mongoClient = new MongoClient( “localhost” , 27017 );

// Now connect to your databases

DB db = mongoClient.getDB( “test” );

System.out.println(“Connect to database successfully”);

boolean auth = db.authenticate(myUserName, myPassword);

System.out.println(“Authentication: “+auth);

}

catch(Exception e){    // catch statement   }

Program can be compiled and executed using following commands :

$ javac MongoDBJDBC.java

$ java -classpath “.:mongo-2.10.1.jar” MongoDBJDBC

Connect to database successfully

Authentication: true

Getting/ selecting a collection:  To get/select a collection from the database, getCollection() method of com.mongodb.DBCollection class is used.

 

Following code snippet is useful for creating collection using JAVA.

//creates a collection

DBCollection collection = db.getCollection(“dummyColl”);

//Create object to represent MongoDB document

BasicDBObject doc = new BasicDBObject();

//store key value pairs in document and insert document in database collection.

doc.put(“officer”, “Andrew Smith”);

doc.put(“location”, “Target Shopping Center parking lot”);

doc.put(“vehicle_plate”, “Virginia 2345”);

doc.put(“offense”, “Double parked”);

doc.put(“date”, “2010/08/13”);

doc.put(“pic”, gfsFile);

collection.insert(doc);

GridFS : GridFS is a specification for storing and retrieving files that exceed the BSON document size limit of 16MB. Instead of storing a file in a single document, GridFS divides a file into parts, or chunks, and stores each of those chunks as a separate document. By default GridFS limits chunk size to 255k. GridFS uses two collections to store files. One collection stores the file chunks, and the other stores file metadata.

When you query a GridFS store for a file, the driver or client will reassemble the chunks as needed. You can perform range queries on files stored through GridFS. You also can access information from arbitrary sections of files, which allows you to “skip” into the middle of a video or audio file.

GridFS is useful not only for storing files that exceed 16MB but also for storing any files for which you want access without having to load the entire file into memory.

Implement GridFS :To store and retrieve files using GridFS, use either of the following:

  • A MongoDB driver.
  • The mongofiles command-line tool in the mongo

GridFS Collections :GridFS stores files in two collections:

  • chunks stores the binary chunks.
  • files stores the file’s metadata.

GridFS places the collections in a common bucket by prefixing each with the bucket name. By default, GridFS uses two collections with names prefixed by fs bucket:

fs.files and fs.chunks

You can choose a different bucket name than fs, and create multiple buckets in a single database.

Each document in the chunks collection represents a distinct chunk of a file as represented in the GridFS store. Each chunk is identified by its unique ObjectId stored in its _id field.

The following is an example of the GridFS interface in Java. The example is for demonstration purposes only.

By default, the interface must support the default GridFS bucket, named fs, as in the following:

// returns default GridFS bucket (i.e. “fs” collection)

GridFS myFS = new GridFS(myDatabase);

 

// saves the file to “fs” GridFS bucket

myFS.createFile(new File(“/tmp/largething.mpg”));

Optionally, interfaces may support other additional GridFS buckets as in the following example:

// returns GridFS bucket named “contracts”

GridFS myContracts = new GridFS(myDatabase, “contracts”);

 

// retrieve GridFS object “smithco”

GridFSDBFile file = myContracts.findOne(“smithco”);

 

// saves the GridFS file to the file system

file.writeTo(new File(“/tmp/smithco.pdf”));

JavaServer Pages(JSP): JavaServer Pages (JSP) is a technology for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>. A JavaServer Pages component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application. Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands. Using JSP, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.

JSP is an integral part of Java EE, a complete platform for enterprise class applications. This means that JSP can play a part in the simplest applications to the most complex and demanding.

Advantages of JSP:

Following is the list of other advantages of using JSP over other technologies:

  • vs. Active Server Pages (ASP): The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.
  • vs. Pure Servlets: It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML.
  • vs. Server-Side Includes (SSI): SSI is really only intended for simple inclusions, not for “real” programs that use form data, make database connections, and the like.
  • vs. JavaScript: JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc.
  • vs. Static HTML: Regular HTML, of course, cannot contain dynamic information.

Sample JSP Program :

Following is the example which uses getHeaderNames() method of HttpServletRequest to read the HTTP header infromation. This method returns an Enumeration that contains the header information associated with the current HTTP request.

Once we have an Enumeration, we can loop down the Enumeration in the standard manner, using hasMoreElements() method to determine when to stop and using nextElement() method to get each parameter name.

<%@ page import=”java.io.*,java.util.*” %>

<html>

<head>

<title>HTTP Header Request Example</title>

</head>

<body>

<center>

<h2>HTTP Header Request Example</h2>

<table width=”100%” border=”1″ align=”center”>

<tr bgcolor=”#949494″>

<th>Header Name</th><th>Header Value(s)</th>

</tr>

<%

Enumeration headerNames = request.getHeaderNames();

while(headerNames.hasMoreElements()) {

String paramName = (String)headerNames.nextElement();

out.print(“<tr><td>” + paramName + “</td>\n”);

String paramValue = request.getHeader(paramName);

out.println(“<td> ” + paramValue + “</td></tr>\n”);

}

%>

</table>

</center>

</body>

</html>

 

Web Server: The Apache Web server provides a full range of Web server features, including CGI, SSL, and virtual domains. Apache also supports plug-in modules for extensibility. Apache is free software, distributed by the Apache Software Foundation that promotes various free and open source advanced Web technologies. Web Server is required for processing JSP pages over server.

Conclusion:

The experiment demonstrates use of JSP to store text and images in MongoDB database using GridFS.

 

************************* PROGRAM ********************

AIM : Use MongoDB to process semi structured and unstructured data collections
 such as Rfid, images, blogs use python/Java MongoDB interface.

 //INDEX.jsp
<%--
    Document   : index
  
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body back>
       
        <a href="Blog.html">Compose your Blog</a><br>
        <a href="ViewBlog.jsp">Display Available Blogs</a> 
       
    </body>
   
</html>
 
//BLOG.html
 
<!DOCTYPE html>
<html>
    <head>
        <title>Blog Page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <form action="BlogSave.jsp" method="POST">
        <table border="0" cellspacing="1" cellpadding="1">
            <tbody>
                <tr>
                    <td>Author</td>
                    <td><input type="text" name="tauth" value="" /></td>
                </tr>
                <tr>
                    <td>Title</td>
                    <td><input type="text" name="ttitle" value="" /></td>
                </tr>
                <tr>
                    <td>Comments</td>
                    <td><textarea name="tcomments" rows="4" cols="20"></textarea></td>
                </tr>
                <tr>
                    <td>Select Image</td>
                    <td><input type="file" name="tpic" /></td>
                </tr>
                <tr>
                    <td>Blog Date</td>
                    <td<input type="text" name="tdate" value="TODAY" readonly="readonly" /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Save Comments" name="blogsave" /></td>
                    <td><input type="reset" value="Clear Comment" name="clearbut" /></td>
                </tr>
                <tr> <a href="index.jsp">Home Page</a></tr>
            </tbody>
        </table>
      
    </form>
   
</html>
 
//BlogSave.jsp
<%--
    Document   : BlogSave
 
--%>
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Write Blog and Save in MongoDB</title>
    </head>
     <body>
        <%@ page import="java.io.*"%>
        <%@ page import="java.net.*"%>
        <%@ page import="com.mongodb.*"%>
        <%@ page import="com.mongodb.gridfs.*"%>
        <%
            try
            {
            MongoClient mongo = new MongoClient("localhost", 27017);
            DB db = mongo.getDB("java");
            DBCollection collection = db.getCollection("blog");
            BasicDBObject document = new BasicDBObject();
            String author=request.getParameter("tauth");
            String title1=request.getParameter("ttitle");
            String comments=request.getParameter("tcomments");
           
            java.util.Date d=new java.util.Date();
            String date=d.toString();
            document.put("Author",author);
            document.put("Title1",title1);
            document.put("Comments",comments);
//            document.put("Pic",gfsFile);
            document.put("DateTime",date);
            collection.save(document);    
           
           
            File imageFile = new File("/home/sonali/Download/"+request.getParameter("tpic"));        
            GridFS gfsPhoto = new GridFS(db,"photo");
            GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);
            gfsFile.setFilename(author);
            gfsFile.save();
            }
            catch(Exception e)
            {       
                out.println(e);      
            }
            %>
            <h1>Record saved Successfully in Database !!!</h1>
    </body>
</html>
 
//ViewBlog.jsp
<%--
    Document   : ViewBlog
   
--%>
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>View MongoDB Contents in JSP</title>
    </head>
    <body>
        <%@ page import="java.io.*"%>
        <%@ page import="java.net.*"%>
        <%@ page import="com.mongodb.*"%>
        <%@ page import="com.mongodb.gridfs.*"%>
        <%
           
            try
            {
            MongoClient mongo = new MongoClient("localhost", 27017);
            DB db = mongo.getDB("java");
            DBCollection collection = db.getCollection("blog");
          
            DBCursor cursor = collection.find();
            GridFS gfsPhoto = new GridFS(db,"photo");
                    
            out.println("<table><tbody><th colspan=2>List of All documents available in Collection Blog From MongoDB</th>");
            while(cursor.hasNext())
                {
                    BasicDBObject dbObject = (BasicDBObject)cursor.next(); 
                    String author=dbObject.getString("Author"); 
                    String title=dbObject.getString("Title1"); 
                    String comments=dbObject.getString("Comments"); 
                    String date=dbObject.getString("DateTime"); 
                   String authpic="/home/sonali/Download/"+author;
                   
                try
                {
                    GridFSDBFile imageForOutput = gfsPhoto.findOne(author);
                    imageForOutput.writeTo(authpic);
                }
                catch(Exception e)
                {
                    out.println("Error creating image"+e);
                }
                    out.println("<table><tr><td>Author</td>");
                    out.println("<td>"+author+"</td></tr>");
                    out.println("<tr><td>Title</td>");
                    out.println("<td>"+title+"</td></tr>");
                    out.println("<tr><td>Comments</td>");
                    out.println("<td>"+comments+"</td></tr>");
                    out.println("<tr><td>Date</td>");
                    out.println("<td>"+date+"</td></tr>");
                    out.println("<tr><td>Image</td>");
                    out.println("<td>"+authpic+"</td></tr>");
                    out.println("<tr><td><img width=20 height=30 border=1 src="+authpic+" /></td>");
                    out.println("<tr><td>****************</td><td>****************</td></tr>");
                 }
                    out.println("</tbody></table>");
       
            }
                     catch(Exception e)
                     {       
                           out.println(e);      
                     }
            %>
    </body>
</html>

******************************** OUTPUT ********************************

Leave a Reply

Your email address will not be published. Required fields are marked *