Mongodb——GridFS

GridFS是一种用于存储和恢复大于16MB文件的技术。它通过将文件拆分为多个块并存储在两个集合中来实现这一点。本文详细介绍了GridFS的工作原理、文件存储结构以及如何使用GridFS API。


GridFS用于存储和恢复那些超过16M(BSON文件限制)的文件。

GridFS将文件分成大块,将每个大块存储为单独的文件.GridFS中限制chunk最大为256k。GridFS使用两个collection存储,一个存储chunks,一个存储元数据(metadata)。
fs.files和fs.chunks


When should I use GridFS?
http://docs.mongodb.org/manual/faq/developers/#faq-developers-when-to-use-gridfs

 


file Collection:具体形式如下
{
  "_id" : <ObjectID>,
  "length" : <num>,
  "chunkSize" : <num>
  "uploadDate" : <timestamp>
  "md5" : <hash>

  "filename" : <string>,
  "contentType" : <string>,
  "aliases" : <string array>,
  "metadata" : <dataObject>,
}

Documents in the files collection contain some or all of the following fields. Applications may create additional arbitrary fields:

files._id
    The unique ID for this document. The _id is of the data type you chose for the original document. The default type for MongoDB documents is BSON ObjectID.

files.length
    The size of the document in bytes.

files.chunkSize
    The size of each chunk. GridFS divides the document into chunks of the size specified here. The default size is 256 kilobytes.

files.uploadDate
    The date the document was first stored by GridFS. This value has the Date type.

files.md5
    An MD5 hash returned from the filemd5 API. This value has the String type.

files.filename
    Optional. A human-readable name for the document.

files.contentType
    Optional. A valid MIME type for the document.

files.aliases
    Optional. An array of alias strings.

files.metadata
    Optional. Any additional information you want to store.


The chunks Collection:举例如下
{
  "_id" : <string>,
  "files_id" : <string>,
  "n" : <num>,
  "data" : <binary>
}

A document from the chunks collection contains the following fields:
chunks._id
    The unique ObjectID of the chunk.

chunks.files_id
    The _id of the “parent” document, as specified in the files collection.

chunks.n
    The sequence number of the chunk. GridFS numbers all chunks, starting with 0.

chunks.data
    The chunk’s payload as a BSON binary type.

GridFS Index

GridFS使用chunks中files_id和n域作为混合索引,files_id是父文档的_id,n域包含chunk的序列号,该值从0开始。
GridFS索引支持快速恢复数据。

cursor = db.fs.chunks.find({files_id: myFileID}).sort({n:1});

如果没有建立索引,可以使用下列shell命令:
db.fs.chunks.ensureIndex( { files_id: 1, n: 1 }, { unique: true } );

Example Interface:

// returns default GridFS bucket (i.e. "fs" collection)
GridFS myFS = new GridFS(myDatabase);

// saves the file to "fs" GridFS bucket
myFS.createFile(new File("/tmp/largething.mpg"));

接口支持额外的GridFS buckets
// returns GridFS bucket named "contracts"
GridFS myContracts = new GridFS(myDatabase, "contracts");

// retrieve GridFS object "smithco"
GridFSDBFile file = myContracts.findOne("smithco");

// saves the GridFS file to the file system
file.writeTo(new File("/tmp/smithco.pdf"));

基于径向基函数神经网络RBFNN的自适应滑模控制学习(Matlab代码实现)内容概要:本文介绍了基于径向基函数神经网络(RBFNN)的自适应滑模控制方法,并提供了相应的Matlab代码实现。该方法结合了RBF神经网络的非线性逼近能力和滑模控制的强鲁棒性,用于解决复杂系统的控制问题,尤其适用于存在不确定性和外部干扰的动态系统。文中详细阐述了控制算法的设计思路、RBFNN的结构与权重更新机制、滑模面的构建以及自适应律的推导过程,并通过Matlab仿真验证了所提方法的有效性和稳定性。此外,文档还列举了大量相关的科研方向和技术应用,涵盖智能优化算法、机器学习、电力系统、路径规划等多个领域,展示了该技术的广泛应用前景。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的研究生、科研人员及工程技术人员,特别是从事智能控制、非线性系统控制及相关领域的研究人员; 使用场景及目标:①学习和掌握RBF神经网络与滑模控制相结合的自适应控制策略设计方法;②应用于电机控制、机器人轨迹跟踪、电力电子系统等存在模型不确定性或外界扰动的实际控制系统中,提升控制精度与鲁棒性; 阅读建议:建议读者结合提供的Matlab代码进行仿真实践,深入理解算法实现细节,同时可参考文中提及的相关技术方向拓展研究思路,注重理论分析与仿真验证相结合。
### Java 中使用 MongoDB GridFS 进行文件操作 #### 创建连接并初始化 GridFSBucket 对象 为了在 Java 应用程序中使用 GridFS 存储和检索文件,首先需要创建一个 `MongoClient` 并指定数据库名称。接着通过该客户端获取目标数据库实例,并基于此实例构建 `GridFSBucket` 对象。 ```java // 导入必要的库 import com.mongodb.client.gridfs.GridFSBuckets; import com.mongodb.client.gridfs.GridFSBucket; public class GridFsExample { public static void main(String[] args) { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase database = mongoClient.getDatabase("testdb"); // 初始化 GridFSBucket 实例 GridFSBucket gridFSBucket = GridFSBuckets.create(database, "myfiles"); } } ``` #### 将本地文件上传至 GridFS 利用 `uploadFromStream()` 方法可以从输入流读取数据并将之保存到 GridFS 中。这里展示了一个简单的例子,说明如何将图片文件存入数据库。 ```java try (InputStream inputStream = Files.newInputStream(Paths.get("path/to/image.jpg"))) { ObjectId fileId = gridFSBucket.uploadFromStream("image_name_in_db", inputStream); System.out.println("File uploaded successfully with ID: " + fileId.toHexString()); } catch (IOException e) { e.printStackTrace(); } ``` #### 下载 GridFS 中的文件 下载文件可以通过调用 `downloadToStream()` 或者 `downloadToStreamByName()`.前者依据 `_id` 字段定位特定记录;后者则允许按自定义的名字查询对应的文件资源。 ```java ObjectId fileId = new ObjectId("file_id_here"); OutputStream outputStream = new FileOutputStream("output_file_path"); gridFSBucket.downloadToStream(fileId, outputStream); outputStream.close(); System.out.println("Download completed."); ``` #### 删除 GridFS 中的文件 删除操作同样简单明了,只需提供相应的 `_id` 即可执行移除动作: ```java ObjectId fileIdToDelete = new ObjectId("file_id_to_delete"); gridFSBucket.delete(fileIdToDelete); System.out.println("Deletion successful."); ``` 以上就是有关于 Java 和 MongoDB GridFS 结合使用的几个基础功能介绍[^1]。这些代码片段展示了怎样完成常见的 CRUD 操作——即创建(Create),读取(Retrieve),更新(Update)(此处未涉及具体实现细节),以及销毁(Destroy)。对于更复杂的应用场景,还可以进一步探索官方 API 文档中的其他特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值