热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在java程序中操作MongoDB存储文件

GridFS是MongoDB为存取大型文档(超过4mb)准备的。首先是关于GridFS原理性的介绍。一、GridFS原理ThisfilesystemwithinMongoDBwasdesignedfor…well,holdingfiles,especiallyfilesover4MB…why4MB?WellBSONob
GridFS是MongoDB为存取大型文档(超过4mb)准备的。首先是关于GridFS原理性的介绍。

一、GridFS原理
This filesystem within MongoDB was designed for … well, holding files, especially files over 4MB … why 4MB?
Well BSON objects are limited to 4MB in size (BSON is the format that MongoDB uses to store it’s database information) so GridFS helps store files across multiple chunks.
As Kristina Chodorow of 10Gen puts it …
GridFS breaks large files into manageable chunks. It saves the
chunks to one collection (fs.chunks) and then metadata about the
file to another collection (fs.files). When you query for the file,
GridFS queries the chunks collection and returns the file one piece
at a time.
Why would you want to break large files in to “chunks”? A lot of of comes down to efficient memory & disk usage.
Say you want to store larger files (maybe a 2GB video) when you preform a query on that file all 2GB needs to be loaded into memory … say you have a bigger file, 10GB, 25GB etc … it’s quite likely you’d run out of usable RAM or not have that much RAM available at all!
So, GridFS solves this problem by streaming the data back (in chunks) to the client … this way you’d never need to use more than 4MB of RAM.

二、命令行方式操作
在MongoDB的安装目录下有个mongoFiles.exe这就是用来存取文件的。

存储之后,发现这个文件被存在了testDatabase的fs.files集合下。利用testDatabase.fs.files.find("mylove.wma")可以查询到此文档。

三、java方式存储
 import java.io.File;  
 import java.net.UnknownHostException;  
 import java.util.Date;  
 import java.util.List;  
 import com.mongodb.BasicDBObject;  
 import com.mongodb.DB;  
 import com.mongodb.DBCollection;  
 import com.mongodb.DBCursor;  
 import com.mongodb.DBObject;  
 import com.mongodb.Mongo;  
 import com.mongodb.MongoException;  
 import com.mongodb.gridfs.GridFS;  
 import com.mongodb.gridfs.GridFSInputFile;  
   
 public class MongoDBClientTest {  
   
     public static void main(String[] args) {  
 //      initData();  
 //      query();  
         initData4GridFS();  
     }  
       
       
     private static void initData4GridFS()   {  
         long start = new Date().getTime();  
         try {  
             Mongo db = new Mongo("127.0.0.1", 50000);  
             DB mydb = db.getDB("wlb");  
             File f = new File("D://study//document//MySQL5.1参考手册.chm");  
             GridFS myFS = new GridFS(mydb);               
             GridFSInputFile inputFile = myFS.createFile(f);  
             inputFile.save();  
   
             DBCursor cursor = myFS.getFileList();  
             while(cursor.hasNext()){  
                 System.out.println(cursor.next());  
             }     
             db.close();  
             long endTime = new Date().getTime();  
             System.out.println(endTime - start);  
             System.out.println((endTime - start) / 10000000);  
         }catch (Exception e) {  
             e.printStackTrace();  
         }  
     }  
 }


推荐阅读
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了Mongodb副本集+分片集群搭建相关的知识,希望对你有一定的参考价值。环境需求: ... [详细]
  • 一、前言在数据库中,慢查询日志通常是用来进行优化数据库,MySQL中存在慢查询,Mongodb中也是如此。在Mongo中的慢查询属于Mon ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 本文介绍了关系型数据库和NoSQL数据库的概念和特点,列举了主流的关系型数据库和NoSQL数据库,同时描述了它们在新闻、电商抢购信息和微博热点信息等场景中的应用。此外,还提供了MySQL配置文件的相关内容。 ... [详细]
  • Allegro总结:1.防焊层(SolderMask):又称绿油层,PCB非布线层,用于制成丝网印板,将不需要焊接的地方涂上防焊剂.在防焊层上预留的焊盘大小要比实际的焊盘大一些,其差值一般 ... [详细]
  • 一面自我介绍对象相等的判断,equals方法实现。可以简单描述挫折,并说明自己如何克服,最终有哪些收获。职业规划表明自己决心,首先自己不准备继续求学了,必须招工作了。希望去哪 ... [详细]
  • step1.为mongodb添加admin管理员root@12.154.29.163:~#mongoMongoDBshellversionv3.4.2connectingto:mo ... [详细]
  • Intellij IDEA中详细图解连接MySQL腾讯云数据库以及基础操作
    虽然小编记录的是在IDEA中连接mysql腾讯云数据库。当然,如果读者使用的是本地数据库,也是一样的操作,只是数据库的url书写有所不同。 ... [详细]
  • nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 今天我们学习,数据库mongodb的使用,最下面有mongodb的下载链接。pipinstallpymongo首先安装pymongo,然后在需要用到的地方importpymongo ... [详细]
  • MongoDB学习:(二)MongoDB简单使用
    MongoDB学习:(二)MongoDB简单使用MongoDB使用:执行mongodb的操作之前,我们需要运行命令,来进入操作命令界面>mongo提示 ... [详细]
  •     系统采用jeeplus框架(ssm+redis+shiro+mongodb+redis),默认是做了JSP未做前后端分离,由于业务需要已经多终端使用的需求(H5、小程序等) ... [详细]
  • mongoDB高可用集群环境搭建
    2019独角兽企业重金招聘Python工程师标准在生产环境下,部署一台mongodb服务的话,会存在以下问题:单点问题生产环境是一个 ... [详细]
  • 这是我的第一篇博客,多有不足请指教,话不多说,直入正题原因:由于mongodb数据量较大,占用空间较大,所以决定转移数据目录到另一块磁盘下一、查看磁盘占用情况[root@test~ ... [详细]
  • 前面刚有AWS开战MongoDB,双方“隔空互呛”,这厢又曝出2亿+简历信息泄露——MongoDB的这场开年似乎“充实”得过分了些。长期以来,作为“最受欢迎的NoSQL数据库”,M ... [详细]
author-avatar
zaizaiwaipo
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有