You are on page 1of 3

PL1(B-4)

import java.io.*;
import java.net.UnknownHostException;
import com.mongodb.*;
import com.mongodb.gridfs.*;
import java.util.*;
public class Blog{
public static byte[] loadimage(String filepath) throws Exception{
File file=new File(filepath);
int size=(int)file.length();
byte[] buffer=new byte[size];
FileInputStream in=new FileInputStream(file);
in.read(buffer);
in.close();
return buffer;
}
public static void main(String args[]) throws Exception
{
Scanner in=new Scanner(System.in);
int ch;
do{
try {
int i;
System.out.println("enter ur choice:\n1.Create blog \n2.Display blogs");
i=in.nextInt();
switch(i)
{
case 1:
// connect to mongoDB, ip and port number
String tx,im,vid,blogname,bname;
System.out.println("Enter name for blog : ");
blogname=in.next();
System.out.println("Enter text for blog : ");
tx=in.next();
System.out.println("Enter image filepath for blog : ");
im=in.next();
System.out.println("Enter videopath for blog : ");
vid=in.next();
byte[] imagebytes=loadimage(""+im+"");
byte[] videobytes=loadimage(""+vid+"");
Mongo mong=new Mongo("127.0.0.1");
String dbs="Gridfsasgn2";
DB db=mong.getDB(dbs);
GridFS fs=new GridFS(db);
//GridFS is the MongoDB specification for storing and retrieving large files
//such as images, audio files, video files, etc. It is kind of a file system to
//store files but its data is stored within MongoDB collections.
//GridFS has the capability to store files even greater than its document size l
imit of 16MB
GridFSInputFile gfin=fs.createFile(imagebytes);
gfin.save();
GridFSInputFile gfin2=fs.createFile(videobytes);
gfin2.save();
DBCollection collcn= db.getCollection("blog");
BasicDBObject doc=new BasicDBObject();
doc.put("blogname",""+blogname+"");
doc.put("text",""+tx+"");
doc.put("image",""+im+"");
doc.put("video",""+vid+"");
doc.put("Hitsbyuser",0);

collcn.insert(doc);
break;
case 2:
System.out.println("Enter name of blog u want to see : ");
bname=in.next();
Mongo mong1=new Mongo("127.0.0.1");
String dbs1="Gridfsasgn2";
DB db1=mong1.getDB(dbs1);
DBCollection collcn1= db1.getCollection("blog");
BasicDBObject doc1=new BasicDBObject();
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("blogname", bname);
DBCursor cursor = collcn1.find(searchQuery);
while(cursor.hasNext())
{
System.out.println(cursor.next());
}
BasicDBObject newdoc=new BasicDBObject().append("$inc",new BasicDBObject().appen
d("Hitsbyuser", 1));
collcn1.updateMulti(new BasicDBObject().append("blogname",bname), newdoc);
//updateMulti will update all matching documents
//BasicDBObject query = new BasicDBObject();
//BasicDBObject fields = new BasicDBObject("blogname",true).append("id",false).a
ppend("Hitsbyuser", true);
//System.out.print("\n 2-d graph for hit users \n\n");
//DBCursor curs = collcn1.find(query, fields);
//while(curs.hasNext()) {
//DBObject o = curs.next();
//System.out.println(o.toString());
//}
break;
default: break;
}
System.out.print("Done all\n");
}
catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
System.out.println("1.continue\t 2. exit: ");
ch=in.nextInt();
}while(ch==1);
}
}
//****************Output**********************
enter ur choice:
1.Create blog
2.Display blogs
1
Enter name for blog :
Anand
Enter text for blog :
IloveMyIndia
Enter image filepath for blog :
/home/sl279/Desktop/pun.jpg
Enter videopath for blog :
/home/sl279/Desktop/111.mp4
Done all
1.continue
2. exit:

1
enter ur choice:
1.Create blog
2.Display blogs
2
Enter name of blog u want to see :
Anand
{ "_id" : { "$oid" : "57d1bc94e4b0ddf2e937cda5"} , "blogname"
: "IloveMyIndia" , "image" : "/home/sl279/Desktop/pun.jpg" ,
l279/Desktop/111.mp4" , "Hitsbyuser" : 0}
Done all
1.continue
2. exit:
1
enter ur choice:
1.Create blog
2.Display blogs
2
Enter name of blog u want to see :
Anand
{ "_id" : { "$oid" : "57d1bc94e4b0ddf2e937cda5"} , "blogname"
: "IloveMyIndia" , "image" : "/home/sl279/Desktop/pun.jpg" ,
l279/Desktop/111.mp4" , "Hitsbyuser" : 1}
Done all
1.continue
2. exit:
//*****************Output********************
sl279@sl279-HP-Compaq-4000-Pro-SFF-PC:~$ mongo
MongoDB shell version: 2.4.9
connecting to: test
> show dbs
Book
0.203125GB
Branches
0.203125GB
Gridfsasgn2
0.203125GB
Nashik 0.203125GB
database
0.203125GB
local 0.078125GB
pvg
0.203125GB
test
0.203125GB
testdb 0.203125GB
> use Gridfsasgn2
switched to db Gridfsasgn2
> db.blog.find().pretty();
{
"_id" : ObjectId("57d1bb15e4b0db20a958e1cf"),
"blogname" : "abc",
"text" : "fgfhhjh",
"image" : "/home/sl279/Desktop/pun.jpg",
"video" : "/home/sl279/Desktop/111.mp4",
"Hitsbyuser" : 2
}
{
"_id" : ObjectId("57d1bc94e4b0ddf2e937cda5"),
"blogname" : "Anand",
"text" : "IloveMyIndia",
"image" : "/home/sl279/Desktop/pun.jpg",
"video" : "/home/sl279/Desktop/111.mp4",
"Hitsbyuser" : 2
}

: "Anand" , "text"
"video" : "/home/s

: "Anand" , "text"
"video" : "/home/s

You might also like