Example usage for com.mongodb.client.gridfs GridFSBuckets create

List of usage examples for com.mongodb.client.gridfs GridFSBuckets create

Introduction

In this page you can find the example usage for com.mongodb.client.gridfs GridFSBuckets create.

Prototype

public static GridFSBucket create(final MongoDatabase database) 

Source Link

Document

Create a new GridFS bucket with the default 'fs' bucket name

Usage

From source file:UnitTest3.java

License:Open Source License

public static void testgridfs2() throws FileNotFoundException, IOException {

    long time1;// w  ww  . j ava  2 s  . c  o  m
    long time2;
    long time3;
    long time4;
    String fileName = "makezip3.rar";

    MongoClient mongo = new MongoClient("localhost", 27017);
    //DB db = mongo.getDB("JFileDB");
    MongoDatabase db = mongo.getDatabase("JFileDB");
    GridFSBucket gridFSBucket = GridFSBuckets.create(db);

    // Get the input stream
    time1 = System.currentTimeMillis();
    InputStream streamToUploadFrom = new FileInputStream("/home/anees/workspace/testdata/in/" + fileName);

    // Create some custom options
    GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024 * 1024)
            .metadata(new Document("type", "class"));

    ObjectId fileId = gridFSBucket.uploadFromStream(fileName, streamToUploadFrom, options);
    streamToUploadFrom.close();
    time2 = System.currentTimeMillis();

    time3 = System.currentTimeMillis();
    Date date = new Date();

    FileOutputStream streamToDownloadTo = new FileOutputStream(
            "/home/anees/workspace/testdata/out/" + fileName + "_" + date.toString());
    // latest file with same name in DB
    GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(-1);
    //original file with same file name in DB GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(0);

    //downloadOptions.
    gridFSBucket.downloadToStreamByName(fileName, streamToDownloadTo, downloadOptions);
    streamToDownloadTo.close();
    time4 = System.currentTimeMillis();

    System.out.println("The fileId of the uploaded file is: " + fileId.toHexString());
    System.out.println("Upload time taken : time2 - time1 : " + (time2 - time1));
    System.out.println("Download time taken : time4 - time3 : " + (time4 - time3));

    /*
     Set the revision of the file to retrieve.
            
    Revision numbers are defined as follows:
            
    0 = the original stored file
    1 = the first revision
    2 = the second revision
    etc..
    -2 = the second most recent revision
    -1 = the most recent revision
            
     */

    mongo.close();

}

From source file:UnitTest3.java

License:Open Source License

public static void testgridfs3() throws FileNotFoundException, IOException {

    long time1;//from   w  ww .j a  v  a 2 s.  c  o  m
    long time2;
    long time3;
    long time4;
    int n = 0;
    // "/home/anees/workspace/testdata/in/App3.class"
    String fileName = "makezip4.rar";

    MongoClient mongo = new MongoClient("localhost", 27017);
    //DB db = mongo.getDB("JFileDB");
    MongoDatabase db = mongo.getDatabase("JFileDB");
    GridFSBucket gridFSBucket = GridFSBuckets.create(db);

    // Get the input stream
    time1 = System.currentTimeMillis();
    InputStream streamToUploadFrom = new FileInputStream("/home/anees/workspace/testdata/in/" + fileName);

    // Create some custom options
    GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024 * 1024)
            .metadata(new Document("type", "class"));

    byte data[] = new byte[64 * 1024 * 1024];
    GridFSUploadStream uploadStream = gridFSBucket.openUploadStream(fileName, options);

    while ((n = streamToUploadFrom.read(data)) > 0) {
        uploadStream.write(data, 0, n);
    }
    uploadStream.close();
    time2 = System.currentTimeMillis();

    time3 = System.currentTimeMillis();
    Date date = new Date();
    FileOutputStream streamToDownloadTo = new FileOutputStream(
            "/home/anees/workspace/testdata/out/" + fileName + "_" + date.toString());
    GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(-1);

    GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStreamByName(fileName, downloadOptions);
    long fileLength = downloadStream.getGridFSFile().getLength();
    byte[] bytesToWriteTo = new byte[64 * 1024 * 1024];

    while ((n = downloadStream.read(bytesToWriteTo)) > 0) {
        streamToDownloadTo.write(bytesToWriteTo, 0, n);
    }
    downloadStream.close();
    time4 = System.currentTimeMillis();

    System.out.println("The fileId of the uploaded file is: " + uploadStream.getFileId().toHexString());
    System.out.println("Upload time taken : time2 - time1 : " + (time2 - time1));
    System.out.println("Download time taken : time4 - time3 : " + (time4 - time3));

    streamToDownloadTo.close();
    streamToUploadFrom.close();
    mongo.close();
}

From source file:UnitTest3.java

License:Open Source License

public static void testgridfs4() throws FileNotFoundException, IOException {

    MongoClient mongo = new MongoClient("localhost", 27017);
    //DB db = mongo.getDB("JFileDB");
    MongoDatabase db = mongo.getDatabase("JFileDB");
    GridFSBucket gridFSBucket = GridFSBuckets.create(db);

    // Get the input stream
    InputStream streamToUploadFrom = new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8));

    // Create some custom options
    GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024)
            .metadata(new Document("type", "file"));

    ObjectId fileId = gridFSBucket.uploadFromStream("test", streamToUploadFrom, options);
    streamToUploadFrom.close();/* w ww.  j a v a 2  s  .  co  m*/
    System.out.println("The fileId of the uploaded file is: " + fileId.toHexString());

    // Get some data to write
    byte[] data = "some data to upload into GridFS".getBytes(StandardCharsets.UTF_8);

    GridFSUploadStream uploadStream = gridFSBucket.openUploadStream("sample_data");
    uploadStream.write(data);
    uploadStream.close();
    System.out.println("The fileId of the uploaded file is: " + uploadStream.getFileId().toHexString());

    /*
    gridFSBucket.find().forEach(new Block<GridFSFile>() {
            
    public void apply(final GridFSFile gridFSFile) {
        System.out.println(gridFSFile.getFilename());
    }
    });
    */

    /*
    gridFSBucket.find(eq("metadata.contentType", "image/png")).forEach(
        new Block<GridFSFile>() {
                    
            public void apply(final GridFSFile gridFSFile) {
                System.out.println(gridFSFile.getFilename());
            }
        });
    */

    FileOutputStream streamToDownloadTo = new FileOutputStream("/tmp/test.txt");
    gridFSBucket.downloadToStream(fileId, streamToDownloadTo);
    streamToDownloadTo.close();

    streamToDownloadTo = new FileOutputStream("/tmp/test.txt");
    GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(0);
    gridFSBucket.downloadToStreamByName("test", streamToDownloadTo, downloadOptions);
    streamToDownloadTo.close();

    GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream(fileId);
    int fileLength = (int) downloadStream.getGridFSFile().getLength();
    byte[] bytesToWriteTo = new byte[fileLength];
    downloadStream.read(bytesToWriteTo);
    downloadStream.close();

    System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8));

    downloadStream = gridFSBucket.openDownloadStreamByName("sample_data");
    fileLength = (int) downloadStream.getGridFSFile().getLength();
    bytesToWriteTo = new byte[fileLength];
    downloadStream.read(bytesToWriteTo);
    downloadStream.close();

    System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8));

    gridFSBucket.rename(fileId, "test2");

    gridFSBucket.delete(fileId);
    mongo.close();
}

From source file:com.dilmus.dilshad.scabi.db.DBackFile.java

License:Open Source License

public DBackFile(DDB ddb) {
    m_ddb = ddb;/*from   www.ja  v  a2s. c  om*/
    m_mongodb = ddb.getDatabase();
    m_chunkSize = 1024 * 1024;
    m_bufferSize = 64 * 1024 * 1024;

    m_table = m_mongodb.getCollection("fs.files");

    // Don't use GridFS() class, raises ClassCastException in with DBCursor logic m_gfs = new GridFS(m_ddb.getDB());

    m_gridFSBucket = GridFSBuckets.create(m_mongodb);

    // Create some custom options
    m_options = new GridFSUploadOptions().chunkSizeBytes(m_chunkSize);

    /* Reference how to add meta-data MongoDB/GridFS specific way
    Document doc = new Document();
    doc.append("Type", "File");
    doc.append("ContentType", "File");
               
    m_options = new GridFSUploadOptions()
        .chunkSizeBytes(m_chunkSize)
        .metadata(doc);
     */

}

From source file:com.dilmus.dilshad.scabi.deprecated.DBackFileOld.java

License:Open Source License

public DBackFileOld(DDBOld ddb) {
    m_ddb = ddb;/*from w w w . j a v a2 s.c om*/
    m_mongodb = ddb.getDatabase();
    m_chunkSize = 1024 * 1024;
    m_bufferSize = 64 * 1024 * 1024;

    m_table = m_ddb.getDB().getCollection("fs.files");

    // Don't use GridFS() class, raises ClassCastException in with DBCursor logic m_gfs = new GridFS(m_ddb.getDB());

    m_gridFSBucket = GridFSBuckets.create(m_mongodb);

    // Create some custom options
    m_options = new GridFSUploadOptions().chunkSizeBytes(m_chunkSize);

    /* Reference how to add meta-data MongoDB/GridFS specific way
    Document doc = new Document();
    doc.append("Type", "File");
    doc.append("ContentType", "File");
               
    m_options = new GridFSUploadOptions()
        .chunkSizeBytes(m_chunkSize)
        .metadata(doc);
     */

}

From source file:com.jaeksoft.searchlib.crawler.cache.MongoDbCrawlCache.java

License:Open Source License

@Override
public void init(String configString) throws IOException {
    rwl.w.lock();/*from ww  w.  j ava 2 s .c o  m*/
    try {
        closeNoLock();
        final MongoClientURI connectionString = new MongoClientURI(configString);
        mongoClient = new MongoClient(connectionString);
        final MongoDatabase database = mongoClient.getDatabase(
                connectionString.getDatabase() == null ? DEFAULT_DATABASE : connectionString.getDatabase());
        metaCollection = database.getCollection(META_COLLECTION);
        metaCollection.createIndex(Document.parse("{\"uri\":1}"));
        indexedCollection = database.getCollection(INDEXED_COLLECTION);
        indexedCollection.createIndex(Document.parse("{\"uri\":1}"));
        contentGrid = GridFSBuckets.create(database);
    } finally {
        rwl.w.unlock();
    }
}

From source file:gridfs.GridFSTour.java

License:Apache License

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes an optional single argument for the connection string
 * @throws FileNotFoundException if the sample file cannot be found
 * @throws IOException if there was an exception closing an input stream
 *///from www .  j  a  v  a  2  s . c o m
public static void main(final String[] args) throws FileNotFoundException, IOException {
    MongoClient mongoClient;

    if (args.length == 0) {
        // connect to the local database server
        mongoClient = new MongoClient();
    } else {
        mongoClient = new MongoClient(new MongoClientURI(args[0]));
    }

    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");
    database.drop();

    GridFSBucket gridFSBucket = GridFSBuckets.create(database);

    /*
     * UploadFromStream Example
     */
    // Get the input stream
    InputStream streamToUploadFrom = new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8));

    // Create some custom options
    GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024)
            .metadata(new Document("type", "presentation"));

    ObjectId fileId = gridFSBucket.uploadFromStream("mongodb-tutorial", streamToUploadFrom, options);
    streamToUploadFrom.close();
    System.out.println("The fileId of the uploaded file is: " + fileId.toHexString());

    /*
     * OpenUploadStream Example
     */

    // Get some data to write
    byte[] data = "Data to upload into GridFS".getBytes(StandardCharsets.UTF_8);

    GridFSUploadStream uploadStream = gridFSBucket.openUploadStream("sampleData");
    uploadStream.write(data);
    uploadStream.close();
    System.out.println("The fileId of the uploaded file is: " + uploadStream.getFileId().toHexString());

    /*
     * Find documents
     */
    gridFSBucket.find().forEach(new Block<GridFSFile>() {
        @Override
        public void apply(final GridFSFile gridFSFile) {
            System.out.println(gridFSFile.getFilename());
        }
    });

    /*
     * Find documents with a filter
     */
    gridFSBucket.find(eq("metadata.contentType", "image/png")).forEach(new Block<GridFSFile>() {
        @Override
        public void apply(final GridFSFile gridFSFile) {
            System.out.println(gridFSFile.getFilename());
        }
    });

    /*
     * DownloadToStream
     */
    FileOutputStream streamToDownloadTo = new FileOutputStream("/tmp/mongodb-tutorial.txt");
    gridFSBucket.downloadToStream(fileId, streamToDownloadTo);
    streamToDownloadTo.close();

    /*
     * DownloadToStreamByName
     */
    streamToDownloadTo = new FileOutputStream("/tmp/mongodb-tutorial.txt");
    GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(0);
    gridFSBucket.downloadToStreamByName("mongodb-tutorial", streamToDownloadTo, downloadOptions);
    streamToDownloadTo.close();

    /*
     * OpenDownloadStream
     */
    GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream(fileId);
    int fileLength = (int) downloadStream.getGridFSFile().getLength();
    byte[] bytesToWriteTo = new byte[fileLength];
    downloadStream.read(bytesToWriteTo);
    downloadStream.close();

    System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8));

    /*
     * OpenDownloadStreamByName
     */

    downloadStream = gridFSBucket.openDownloadStreamByName("sampleData");
    fileLength = (int) downloadStream.getGridFSFile().getLength();
    bytesToWriteTo = new byte[fileLength];
    downloadStream.read(bytesToWriteTo);
    downloadStream.close();

    System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8));

    /*
     * Rename
     */
    gridFSBucket.rename(fileId, "mongodbTutorial");

    /*
     * Delete
     */
    gridFSBucket.delete(fileId);

    database.drop();
}

From source file:org.apache.nifi.processors.mongodb.gridfs.AbstractGridFSProcessor.java

License:Apache License

protected GridFSBucket getBucket(FlowFile input, ProcessContext context) {
    final String name = getBucketName(input, context);
    if (StringUtils.isEmpty(name)) {
        return GridFSBuckets.create(getDatabase(input, context));
    } else {//from  w w  w. j  av a 2 s . c  o  m
        return GridFSBuckets.create(getDatabase(input, context), name);
    }
}