Use MongoDB to store files based on gridfs

Source: Internet
Author: User
Tags mongodump

There are two ways to store files in MongoDB in the gridfsb mode: 1. Command Line program files 2. client-side driver programming (using Java as an example)

1. Command Line Program Files

You can insert file data to the MongoDB database under the command line by running the command 'cmdfiles.exe 'command in the bindirectory. The preceding four shards are used as an example to describe how to simulate a Windows Cluster with three replica sets, three configsevers, and one route node.

D:/mongodb-win32-i386-1.8.0/bin> export files.exe-host 127.0.0.1: 50000-d wlb put into dump.exe
Connected to: 127.0.0.1: 50000
Added file: {_ ID: objectid ('authorization'), filename: "mongodump.exe", chunksize: 262144, uploaddate: new date (1301404609850), MD5: "647dcae88077e6
Fa2bb3be6b0cdebb07 ", Length: 3271680}
Done!

Insert a file to the database WLB, that is, MongoDB/bin/mongodump.exe. The routing node of the MongoDB cluster used is 127.0.0.1, port is 50000, put is the command, it indicates uploading files to MongoDB, and there are also get and delete commands, which indicate obtaining and deleting files respectively.

Run the following command to start Mongo, enter the WLB database, and run dB. fs. Files. Find () to view the file list in gridfs.

D:/mongodb-win32-i386-1.8.0/CMD> Cd D:/mongodb-win32-i386-1.8.0/bin
D:/mongodb-win32-i386-1.8.0/bin> call cmd.exe 127.0.0.1: 50000/admin
MongoDB shell version: 1.8.0
Connecting to: 127.0.0.1: 50000/admin
> Use WLB
Switched to DB WLB
> DB. fs. Files. Find ()
{"_ Id": objectid ("4d91dbc1fb02c495a0a479d7"), "FILENAME": "mongodump.exe", "chunksize": 262144, "uploaddate": isodate ("2011-03-29t13: 16: 49.850z ")," MD5 ":
"647dcae88077e6fa2bb3be6b0cdebb07", "length": 3271680}

2. Access files using Java APIs

Here the mongo-2.5.jar as an example, the code is as follows:

Package com. zhangzk. mongodb; <br/> import java. io. file; <br/> import java.net. unknownHostException; <br/> import java. util. date; <br/> import java. util. list; <br/> import com. mongodb. basicDBObject; <br/> import com. mongodb. DB; <br/> import com. mongodb. DBCollection; <br/> import com. mongodb. DBCursor; <br/> import com. mongodb. DBObject; <br/> import com. mongodb. mongo; <br/> import com. mongodb. except exception; <br/> import com. mongodb. gridfs. gridFS; <br/> import com. mongodb. gridfs. gridFSInputFile; </p> <p> public class MongoDBClientTest {</p> <p> public static void main (String [] args) {<br/> // initData (); <br/> // query (); <br/> initData4GridFS (); <br/>}</p> <p> private static void initData4GridFS () {<br/> long start = new Date (). getTime (); <br/> try {<br/> Mongo db = new Mongo ("127.0.0.1", 50000); <br/> DB mydb = db. getDB ("wlb"); <br/> File f = new File ("D: // study // document // MySQL5.1 reference manual. chm "); <br/> GridFS myFS = new GridFS (mydb); <br/> GridFSInputFile inputFile = myFS. createFile (f); <br/> inputFile. save (); </p> <p> DBCursor cursor = myFS. getFileList (); <br/> while (cursor. hasNext () {<br/> System. out. println (cursor. next (); <br/>}< br/> db. close (); <br/> long endTime = new Date (). getTime (); <br/> System. out. println (endTime-start); <br/> System. out. println (endTime-start)/10000000); <br/>} catch (Exception e) {<br/> e. printStackTrace (); <br/>}< br/>}

Related Article

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.