Q:Use MongoDB to process semi structured and unstructured data collections such as Rfid, images, blogs use python/Java MongoDB interface.
Testing Environment
Mongo DB version : 2.6.3
Network : switched LAN(Ethernet)
IDE: Eclipse (Java EE)
OS: Fedora 20 (64 bit )
Code
import com.mongodb.MongoClient; import com.mongodb.MongoException; import com.mongodb.WriteConcern; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import com.mongodb.DBCursor; import com.mongodb.ServerAddress; import com.mongodb.gridfs.GridFS; import com.mongodb.gridfs.GridFSDBFile; import com.mongodb.gridfs.GridFSInputFile; import java.io.File; import java.util.Arrays; public class MongoDBJDBC { public static void main(String args[]){ /*DB db=(new MongoClient("192.168.4.91",27017)).getDB("te3101"); DBCollection dbcollection=db.getCollection("chanel"); BasicDBObject basicDBObject=new BasicDBObject(); basicDBObject.put("name", "dhiraj"); basicDBObject.put("subscription", 4100); dbcollection.insert(basicDBObject);*/ try{ // To connect to mongodb server MongoClient mongoClient = new MongoClient( "192.168.4.91" , 27017 ); // Now connect to your databases DB db = mongoClient.getDB( "te3101db" ); System.out.println("Connect to database successfully"); boolean auth = db.authenticate("te3101", new String("te3101").toCharArray()); System.out.println("Authentication: "+auth); System.out.println("List of images currently in the database is:"); listImage(db); storeImage(db); System.out.println("List of images after storing the image is:"); listImage(db); getImage(db); deleteImage(db); System.out.println("List of images after deleting the image is:"); listImage(db); }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } public static void storeImage(DB db){ db.getCollection("imagecollection"); File imageFile = new File("/home/te/3101/image2.jpg"); GridFS gfsPhoto = new GridFS(db, "photo"); try{ GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); gfsFile.setFilename("newphoto"); gfsFile.save(); } catch (Exception e){System.out.println("Caught an exception in store image");} } public static void getImage(DB db){ db.getCollection("imagecollection"); try{ GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne("newphoto"); imageForOutput.writeTo("/home/te/3101/mongomage.jpg"); }catch(Exception e){System.out.println("Error while getting image");} }; public static void deleteImage(DB db){ db.getCollection("imagecollection"); try{ GridFS gfsPhoto = new GridFS(db, "photo"); gfsPhoto.remove(gfsPhoto.findOne("newphoto")); }catch(Exception e){System.out.println("Error while deleteing the collection");} }; public static void listImage(DB db){ db.getCollection("imagecollection"); try{ GridFS gfsPhoto = new GridFS(db, "photo"); DBCursor cursor = gfsPhoto.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); } }catch(Exception e){System.out.println("Listing the images error");} }; }
Output
Connect to database successfully
Authentication: true
List of images currently in the database is:
List of images after storing the image is:
{ "_id" : { "$oid" : "57c6a29483145a4fc32d704f"} , "chunkSize" : 262144 , "length" : 25124 , "md5" : "fdfd55e4120b8caa10dfd3b35d6a0b33" , "filename" : "newphoto" , "contentType" : null , "uploadDate" : { "$date" : "2016-08-31T09:25:40.304Z"} , "aliases" : null }
List of images after deleting the image is:
Authentication: true
List of images currently in the database is:
List of images after storing the image is:
{ "_id" : { "$oid" : "57c6a29483145a4fc32d704f"} , "chunkSize" : 262144 , "length" : 25124 , "md5" : "fdfd55e4120b8caa10dfd3b35d6a0b33" , "filename" : "newphoto" , "contentType" : null , "uploadDate" : { "$date" : "2016-08-31T09:25:40.304Z"} , "aliases" : null }
List of images after deleting the image is:
Note
The programming style used in this code simply sucks. I never develop large project using such crappy code. I ask you not to do the same. Though I should never be writing code this way, time constraints make me do so. The is the poorest way, any one code in fact, could handle exceptions.
Moral: Ensure that you give programmers sufficient time. What's good takes time.
Thanks for sharing this amazing information.Nice blog
ReplyDeleteweb design agency uk
web design agency uk
web design agency uk
web design agency uk
web design agency uk