本文整理了Java中com.mongodb.gridfs.GridFS
类的一些代码示例,展示了GridFS
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。GridFS
类的具体详情如下:
包路径:com.mongodb.gridfs.GridFS
类名称:GridFS
[英]Implementation of GridFS - a specification for storing and retrieving files that exceed the BSON-document size limit of 16MB.
Instead of storing a file in a single document, GridFS divides a file into parts, or chunks, and stores each of those chunks as a separate document. By default GridFS limits chunk size to 255k. GridFS uses two collections to store files. One collection stores the file chunks, and the other stores file metadata.
When you query a GridFS store for a file, the driver or client will reassemble the chunks as needed. You can perform range queries on files stored through GridFS. You also can access information from arbitrary sections of files, which allows you to "skip" into the middle of a video or audio file.
GridFS is useful not only for storing files that exceed 16MB but also for storing any files for which you want access without having to load the entire file into memory. For more information on the indications of GridFS, see MongoDB official documentation.
[中]GridFS的实现—用于存储和检索超过BSON文档大小限制16MB的文件的规范。
GridFS没有将文件存储在单个文档中,而是将文件划分为多个部分或块,并将每个块存储为单独的文档。默认情况下,GridFS将块大小限制为255k。GridFS使用两个集合来存储文件。一个集合存储文件块,另一个存储文件元数据。
当您在GridFS存储区中查询文件时,驱动程序或客户端将根据需要重新组合块。您可以对通过GridFS存储的文件执行范围查询。您还可以从文件的任意部分访问信息,这允许您“跳过”到视频或音频文件的中间。
GridFS不仅适用于存储超过16MB的文件,还适用于存储您想要访问的任何文件,而无需将整个文件加载到内存中。有关GridFS指示的更多信息,请参阅MongoDB官方文档。
代码示例来源:origin: org.mongodb/mongo-java-driver
@SuppressWarnings("deprecation") // We know GridFS uses the old API. A new API version will be address later.
private static GridFS getGridFS() throws Exception {
if (gridFS == null) {
gridFS = new GridFS(getMongo().getDB(db));
}
return gridFS;
}
代码示例来源:origin: org.mongodb/mongo-java-driver
DBCursor fileListCursor = fs.getFileList();
try {
while (fileListCursor.hasNext()) {
GridFS fs = getGridFS();
String fn = args[i + 1];
GridFSDBFile f = fs.findOne(fn);
if (f == null) {
System.err.println("can't find file: " + fn);
GridFS fs = getGridFS();
String fn = args[i + 1];
GridFSInputFile f = fs.createFile(new File(fn));
f.save();
f.validate();
return;
GridFS fs = getGridFS();
String fn = args[i + 1];
GridFSDBFile f = fs.findOne(fn);
if (f == null) {
System.err.println("can't find file: " + fn);
代码示例来源:origin: org.mongodb/mongo-java-driver
/**
* Removes all files matching the given filename.
*
* @param filename the name of the file to be removed
* @throws com.mongodb.MongoException if the operation fails
*/
public void remove(final String filename) {
if (filename == null) {
throw new IllegalArgumentException("filename can not be null");
}
remove(new BasicDBObject("filename", filename));
}
代码示例来源:origin: org.apache.camel/camel-mongodb-gridfs
if (ptsCollection.count() < 1000) {
ptsCollection.createIndex(new BasicDBObject("id", 1));
persistentTimestamp = ptsCollection.findOne(new BasicDBObject("id", endpoint.getPersistentTSObject()));
if (persistentTimestamp == null) {
persistentTimestamp = new BasicDBObject("id", endpoint.getPersistentTSObject());
fromDate = new java.util.Date();
persistentTimestamp.put("timestamp", fromDate);
file = endpoint.getGridFs().findOne(new BasicDBObject("_id", file.getId()));
代码示例来源:origin: org.mongodb/mongo-java-driver
/**
* Finds one file matching the given objectId.
*
* @param objectId the objectId of the file stored on a server
* @return a gridfs file
* @throws com.mongodb.MongoException if the operation fails
*/
public GridFSDBFile findOne(final ObjectId objectId) {
return findOne(new BasicDBObject("_id", objectId));
}
代码示例来源:origin: richardwilly98/elasticsearch-river-mongodb
logger.info("MongoDBRiver is beginning initial import of " + collection.getFullName());
boolean inProgress = true;
String lastId = null;
if (logger.isTraceEnabled()) {
logger.trace("Collection {} - count: {}", collection.getName(), safeCount(collection, timestamp.getClass()));
.find(getFilterForInitialImport(definition.getMongoCollectionFilter(), lastId))
.sort(new BasicDBObject("_id", 1));
while (cursor.hasNext() && context.getStatus() == Status.RUNNING) {
DBObject object = cursor.next();
GridFS grid = new GridFS(mongoClient.getDB(definition.getMongoDb()), definition.getMongoCollection());
cursor = grid.getFileList();
while (cursor.hasNext()) {
DBObject object = cursor.next();
if (object instanceof GridFSDBFile) {
GridFSDBFile file = grid.findOne(new ObjectId(object.get(MongoDBRiver.MONGODB_ID_FIELD).toString()));
if (cursor.hasNext()) {
lastId = addInsertToStream(null, file);
代码示例来源:origin: org.mongodb/mongo-java-driver
/**
* Finds a list of files matching the given filename.
*
* @param filename the filename to look for
* @return list of gridfs files
* @throws com.mongodb.MongoException if the operation fails
*/
public List<GridFSDBFile> find(final String filename) {
return find(new BasicDBObject("filename", filename));
}
代码示例来源:origin: Findwise/Hydra
@Override
public boolean save(Object id, String fileName, InputStream file) {
pipelinefs.remove(new BasicDBObject(MongoDocument.MONGO_ID_KEY, id));
GridFSInputFile inputFile = pipelinefs.createFile(file, fileName);
inputFile.put("_id", id);
inputFile.save();
return true;
}
代码示例来源:origin: stackoverflow.com
String mystring = new String(); // an empty string
GridFS gridFS = new GridFS(mongoTemplate.getDB(),"noteAndFile");
GridFSInputFile gfsFile = gridFS.createFile(
new ByteArrayInputStream( mystring.getBytes() )
);
BasicDBObject meta = new BasicDBObject();
meta.put("comments","hi");
gfsFile.put("metadata",meta);
gfsFile.save();
System.out.println(gfsFile.getId()); // gives me the _id of the object saved
代码示例来源:origin: Findwise/Hydra
@Override
@Deprecated
public void removeInactiveFiles() {
BasicDBObject query = new BasicDBObject();
query.put(MongoPipelineReader.ACTIVE_KEY, Stage.Mode.INACTIVE.toString());
List<GridFSDBFile> list = pipelinefs.find(query);
for(GridFSDBFile file : list) {
pipelinefs.remove(file);
}
}
代码示例来源:origin: com.cognifide.aet/datastorage
@Override
public Artifact getArtifact(DBKey dbKey, String objectID) {
Artifact artifact = null;
GridFS gfs = getGridFS(dbKey);
BasicDBObject query = new BasicDBObject();
query.put(ID_FIELD_NAME, new ObjectId(objectID));
GridFSDBFile file = gfs.findOne(query);
if (file != null) {
artifact = new Artifact(file.getInputStream(), file.getContentType());
}
return artifact;
}
代码示例来源:origin: richardwilly98/elasticsearch-river-mongodb
entry.put(MongoDBRiver.OPLOG_OBJECT, object = new BasicDBObject(MongoDBRiver.MONGODB_ID_FIELD, objectId));
throw new NullPointerException(MongoDBRiver.MONGODB_ID_FIELD);
GridFS grid = new GridFS(mongoShardClient.getDB(definition.getMongoDb()), collection);
GridFSDBFile file = grid.findOne(new ObjectId(objectId));
if (file != null) {
logger.trace("Caught file: {} - {}", file.getId(), file.getFilename());
代码示例来源:origin: org.apache.camel/camel-mongodb-gridfs
GridFSInputFile gfsFile = endpoint.getGridFs().createFile(ins, filename, true);
if (chunkSize != null && chunkSize > 0) {
gfsFile.setChunkSize(chunkSize);
gfsFile.setContentType(ct);
gfsFile.setMetaData(dbObject);
} else if ("remove".equals(operation)) {
final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
endpoint.getGridFs().remove(filename);
} else if ("findOne".equals(operation)) {
final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
GridFSDBFile file = endpoint.getGridFs().findOne(filename);
if (file != null) {
exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
DBCursor cursor;
if (filename == null) {
cursor = endpoint.getGridFs().getFileList();
} else {
cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
DBCursor cursor;
if (filename == null) {
cursor = endpoint.getGridFs().getFileList();
} else {
cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
代码示例来源:origin: Findwise/Hydra
@Override
public void deleteAll() {
documents.remove(new BasicDBObject());
documentfs.remove(new BasicDBObject());
}
代码示例来源:origin: org.apache.jackrabbit/oak-mongomk
private String saveBlob() throws IOException {
BufferedInputStream bis = new BufferedInputStream(is);
String md5 = calculateMd5(bis);
GridFSDBFile gridFile = gridFS.findOne(new BasicDBObject("md5", md5));
if (gridFile != null) {
is.close();
return md5;
}
GridFSInputFile gridFSInputFile = gridFS.createFile(bis, true);
gridFSInputFile.save();
return gridFSInputFile.getMD5();
}
代码示例来源:origin: Findwise/Hydra
@Override
public boolean deleteFile(Object id) {
DBObject obj = new BasicDBObject(MongoDocument.MONGO_ID_KEY, id);
if (pipelinefs.find(obj).size()==0) {
return false;
}
pipelinefs.remove(obj);
return true;
}
}
代码示例来源:origin: com.commercehub.jclouds/jclouds-gridfs-blobstore
private static GridFSDBFile getMostRecentlyUploadedFile(GridFS gridFS, String filename) {
DBObject queryByFilename = new BasicDBObject("filename", filename);
DBObject sortByUploadDateDescending = new BasicDBObject("uploadDate", -1);
DBCursor dbCursor = gridFS.getFileList(queryByFilename, sortByUploadDateDescending);
return dbCursor.hasNext() ? getGridFSDBFileForDBObject(gridFS, dbCursor.next()) : null;
}
代码示例来源:origin: org.mongodb.mongo-hadoop/mongo-hadoop-core
MongoClientURI inputURI = MongoConfigUtil.getInputURI(conf);
GridFS gridFS = new GridFS(
inputCollection.getDB(),
inputCollection.getName());
for (GridFSDBFile file : gridFS.find(query)) {
代码示例来源:origin: xbwen/bugu-mongo
public String save(InputStream is, String filename, Map<String, Object> attributes){
GridFSInputFile f = fs.createFile(is);
f.setChunkSize(chunkSize);
f.setFilename(filename);
setAttributes(f, attributes);
f.save();
return f.getId().toString();
}
代码示例来源:origin: org.mongodb.mongo-hadoop/mongo-hadoop-core
private GridFS getGridFS() {
if (null == gridFS) {
DBCollection rootCollection =
MongoConfigUtil.getCollection(inputURI);
gridFS = new GridFS(
rootCollection.getDB(), rootCollection.getName());
}
return gridFS;
}
内容来源于网络,如有侵权,请联系作者删除!