Java access to MongoDB files

Source: Internet
Author: User
MongoDB has been a constant concern. What is the legendary document database better than MySQL every day? All of this requires data to be used to talk about it. Let's talk nonsense. Let's take a look at a piece of code first. Please ignore the skills and rigor of code writing. Thank you. Packagecom.cn. javabloger; importjava. io. File; importjava. io. iow.t

MongoDB has been a constant concern. What is the legendary document database better than MySQL every day? All of this requires data to be used to talk about it. Let's talk nonsense. Let's take a look at a piece of code first. Please ignore the skills and rigor of code writing. Thank you. Package com.cn. javabloger; import java. io. File; import java. io. IOExcept

MongoDB has been a constant concern. What is the legendary document database better than MySQL every day? All of this requires data,
To put it bluntly, read a piece of code first,Please ignore the skills and rigor of my code writing. Thank you..
Package com.cn. javabloger;

Import java. io. File;
Import java. io. IOException;
Import java. util. Date;
Import java. util. List;

Import com. mongodb. DB;
Import com. mongodb. Mongo;
Import com. mongodb. gridfs. GridFS;
Import com. mongodb. gridfs. GridFSDBFile;
Import com. mongodb. gridfs. GridFSFile;

Public class MongoDBFileTest {

Static DB imgDB = null;
Static GridFS gridFS = null;

Public static void main (String [] args) throws Exception {
Long start = System. currentTimeMillis ();
Mongo mongo = new Mongo ("192.168.50.215", 27017); // database address, Port Number
ImgDB = mongo. getDB ("imgs"); // Database Name
GridFS = new GridFS (imgDB );

String fileName = "android-sdk-windows.rar"; // read 428 M Files
String fileNameO = "spring.zip"; // name of the written file
MongoDBFileTest mon = new MongoDBFileTest ();
File fileIN = new File ("c:/" + fileName );
File fileOUT = new File ("c:/" + fileNameO );

/**
* Save the file to the MongoDB database.
*/
Mon. saveFile (fileIN, fileName );
Long end = System. currentTimeMillis ();
System. out. println (end-start );

/**
* Read data from MongoDB and write data to disk
*/
List List = mon. findFilesByName (fileName );
GridFSDBFile gridFSDBFile = (GridFSDBFile) list. get (0 );
GridFSDBFile. writeTo (fileOUT );
System. out. println (list. size ());
}

/**
* Writing files
* @ Param fileName
*/
Public void saveFile (File file, String fileName ){
Try {
GridFSFile same file = gridFS. createFile (file );
Using file. put ("filename", fileName );
Using file. put ("uploadDate", new Date ());
Using file. put ("contentType", fileName. substring (fileName. lastIndexOf (".")));
Using file. save ();
} Catch (IOException e ){
E. printStackTrace ();
}
}

/**
* Reading files
* @ Param fileName
*/
Public List FindFilesByName (String fileName ){
List List = gridFS. find (fileName );
Return list;
}
}

I started MongoDB on a 32-bit machine,And addedSmallfilesAndNssizeStartup parameters, Find a 428M android-sdk-windows.rar file, write the MongoDB database four times, each write consumes time is87360/69485/105595/40094The memory usage for each insert is higher than once. The mongod process occupies 256 MB, and the mongod process occupies MB of memory. When I write the fifth time, the following error message is displayed:
Tue May 18 00:36:59 Caught Assertion in insert, continuing
Tue May 18 00:36:59 insert imgs. fs. chunks exception userassert: can't map file memory-mongo requires 64 bit build for larger datasets 0 ms
Mmap () failed for/work/fetch data/imgs.7 len: 536870912 errno: 12 Cannot allocate memory
Mmap failed with out of memory, if you're using 32-bits, then you probably need to upgrade to 64

There is no 64-bit system at the moment, and one will be prepared in the near future.

-End-

Original article address: Java's access to MongoDB files. Thank you for sharing it with the original author.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.