Example usage for com.mongodb.gridfs GridFS GridFS

List of usage examples for com.mongodb.gridfs GridFS GridFS

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFS GridFS.

Prototype

public GridFS(final DB db) 

Source Link

Document

Creates a GridFS instance for the default bucket "fs" in the given database.

Usage

From source file:com.seajas.search.profiler.service.repository.RepositoryService.java

License:Open Source License

/**
 * Default constructor.//from   www. j  av  a  2 s. c o  m
 *
 * @param dbFactory
 */
@Autowired
public RepositoryService(final MongoDbFactory dbFactory, final MongoTemplate mongoTemplate) {
    this.gridFs = new GridFS(dbFactory.getDb());

    // Initialize the indexes only if there aren't too many documents in the store yet

    Long currentCount = mongoTemplate.count(new Query(), CompositeEntry.class);

    // Determine whether the given indexes have been created, and initialize them if necessary

    if (currentCount <= MAX_INDEX_CREATION_COUNT)
        for (String index : INDEXES)
            mongoTemplate.indexOps(CompositeEntry.class).ensureIndex(new Index().on(index, Order.ASCENDING));

    this.mongoTemplate = mongoTemplate;
}

From source file:com.softwear.plugins.mongodb.MongoDBScriptObject.java

License:BSD License

public GridFS js_getGridFS(DB db) {
    GridFS fs = new GridFS(db);
    return fs;
}

From source file:com.test.mavenproject1.Main.java

public static void main(String[] args) throws Exception {
    //Load our image
    byte[] imageBytes = LoadImage("/home/fabrice/Pictures/priv/DSCN3338.JPG");
    //Connect to database
    Mongo mongo = new Mongo("127.0.0.1");
    String dbName = "GridFSTestJava";
    DB db = mongo.getDB(dbName);/*from  w w w.  ja  v a  2 s  .  c o  m*/
    //Create GridFS object
    GridFS fs = new GridFS(db);
    //Save image into database
    GridFSInputFile in = fs.createFile(imageBytes);
    in.save();

    //Find saved image
    GridFSDBFile out = fs.findOne(new BasicDBObject("_id", in.getId()));

    //Save loaded image from database into new image file
    FileOutputStream outputImage = new FileOutputStream("/home/fabrice/Pictures/DSCN3338Copy.JPG");
    out.writeTo(outputImage);
    outputImage.close();
}

From source file:com.trenako.images.ImagesRepositoryImpl.java

License:Apache License

private GridFS getGridFs() {
    return new GridFS(dbFactory.getDb());
}

From source file:de.iew.imageread.Main.java

License:Apache License

public void run() {
    try {/*w  w w  .java  2 s . c  o m*/
        this.imageOutputDir = System.getProperty("java.io.tmpdir");

        File outputBase = new File(this.imageOutputDir);
        testAndCreateDirectory(outputBase);

        MongoClient mongoClient = new MongoClient(this.mongohost, this.mongoport);

        DB db = mongoClient.getDB(this.mongodb);

        GridFS gridFS = new GridFS(db);

        DBCursor cursor = gridFS.getFileList(this.queryFromOptions()).sort(new BasicDBObject("uploadDate", -1));

        int imageNumber = 0;
        while (cursor.hasNext()) {
            DBObject fileObject = cursor.next();

            GridFSDBFile file = gridFS.find((ObjectId) fileObject.get("_id"));
            printGridFSDBFile(file);

            if (writeImageFile(outputBase, imageNumber, file)) {
                imageNumber++;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.umn.biomedicus.uima.xmi.MongoDbXmiWriter.java

License:Apache License

@Override
public void initialize(UimaContext aContext) throws ResourceInitializationException {
    super.initialize(aContext);

    String mongoServer = (String) aContext.getConfigParameterValue(PARAM_MONGO_SERVER);
    int mongoPort = (Integer) aContext.getConfigParameterValue(PARAM_MONGO_PORT);
    String mongoDbName = (String) aContext.getConfigParameterValue(PARAM_MONGO_DB_NAME);

    try {//from  w  ww.  ja  v a2 s .  c o m
        mongoClient = new MongoClient(mongoServer, mongoPort);
    } catch (UnknownHostException e) {
        throw new ResourceInitializationException(e);
    }

    DB db = mongoClient.getDB(mongoDbName);

    gridFS = new GridFS(db);
}

From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java

License:EUPL

/**
 * Counts the files stored in the specified name space.
 * @param namespace - (optional) name space whose files are counted
 * @return the number of objects stored in the collection
 *///from   ww  w.jav a  2  s.c  o  m
public long countFiles(final @Nullable String namespace) {
    String namespace2 = trimToNull(namespace);
    if (namespace2 == null) {
        final DB db = client().getDB(CONFIG_MANAGER.getDbName());
        final GridFS gfsNs = new GridFS(db);
        namespace2 = gfsNs.getBucketName();
    }
    return count(namespace2 + "." + GRIDFS_FILES_COLLECTION);
}

From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBConnector.java

License:EUPL

/**
 * Writes statistics about a files name space to the specified output stream.
 * @param os - the output stream to write the statistics to
 * @param namespace - (optional) name space from which the statistics are collected
 * @throws IOException - If an I/O error occurred
 *//*from ww w  .j  a  v  a  2s .  com*/
public void statsFiles(final OutputStream os, final @Nullable String namespace) throws IOException {
    String namespace2 = trimToNull(namespace);
    if (namespace2 == null) {
        final DB db = client().getDB(CONFIG_MANAGER.getDbName());
        final GridFS gfsNs = new GridFS(db);
        namespace2 = gfsNs.getBucketName();
    }
    stats(os, namespace2 + "." + GRIDFS_FILES_COLLECTION);
}

From source file:hulop.hokoukukan.utils.MongoAdapter.java

License:Open Source License

public MongoAdapter(String url, String dbName, String cert) throws Exception {
    mongoURI = cert != null ? new MongoClientURI(url, optionsBuilder(cert)) : new MongoClientURI(url);
    client = new MongoClient(mongoURI);
    db = client.getDB(dbName != null ? dbName : mongoURI.getDatabase());
    mFS = new GridFS(db);
    System.out.println(db.getCollectionNames());
    mapCol = db.getCollection("maps");
    userCol = db.getCollection("users");
    logCol = db.getCollection("logs");
    fileCol = db.getCollection("files");
    entryCol = db.getCollection("entries");

    mapCol.createIndex(new BasicDBObject("geometry", "2dsphere"));
}

From source file:nl.minbzk.dwr.zoeken.enricher.processor.TikaProcessor.java

License:Open Source License

/**
 * Retrieve a grid stream from GridFS.//from ww w. j av a2s.c o  m
 *
 * @param id
 * @return InputStream
 */
private InputStream retrieveGridStream(final String id) {
    if (this.gridFs == null)
        this.gridFs = new GridFS(dbFactory.getDb());

    GridFSDBFile file = gridFs.find(new ObjectId(id));

    return file.getInputStream();
}