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, final String bucketName) 

Source Link

Document

Create a new GridFS bucket with a custom bucket name

Usage

From source file:com.imaginea.mongodb.services.impl.GridFSServiceImpl.java

License:Apache License

/**
 * Service implementation for creating GridFS store in the specified database.
 *
 * @param dbName Name of Database/*from ww w . j av  a 2 s.  co m*/
 * @param bucketName Name of GridFS Bucket
 * @return Status message.
 */
public String createStore(String dbName, String bucketName)
        throws DatabaseException, CollectionException, GridFSException {
    if (dbName == null) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Is Null");
    }
    if (dbName.equals("")) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty");
    }
    if (bucketName == null) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Bucket name is null");
    }
    if (bucketName.equals("")) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Bucket Name Empty");
    }
    if (getAllBuckets(dbName).contains(bucketName)) {
        throw new CollectionException(ErrorCodes.COLLECTION_ALREADY_EXISTS,
                "Collection [" + bucketName + "] already exists in Database [" + dbName + "]");
    } else {
        try {

            GridFSBucket gridFSBucket = GridFSBuckets.create(mongoInstance.getDatabase(dbName), bucketName);

            // Get the input stream
            InputStream streamToUploadFrom = new FileInputStream(new File("/home/maheshk/sample.txt"));

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

            ObjectId fileId = gridFSBucket.uploadFromStream("temp-file", streamToUploadFrom, options);

        } catch (MongoException e) {
            throw new GridFSException(ErrorCodes.GRIDFS_CREATION_EXCEPTION, e.getMessage());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "GridFS bucket [" + bucketName + "] added to database [" + dbName + "].";
    }
}

From source file:com.imaginea.mongodb.services.impl.GridFSServiceImpl.java

License:Apache License

/**
 * Service implementation for getting the list of files stored in GridFS of specified database.
 *
 * @param dbName Name of Database/*from  w  ww  .j av a  2 s  .co m*/
 * @param bucketName Name of GridFS Bucket
 * @param bucketType
 * @param command
 * @param query
 * @param skip
 * @param limit
 * @param sortBy
 * @return JSON representation of list of all files as a String.
 */
public JSONObject executeQuery(String dbName, String bucketName, String bucketType, String command,
        String query, String skip, String limit, String sortBy) throws ApplicationException, JSONException {
    if (dbName == null) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Is Null");
    }
    if (dbName.equals("")) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty");
    }
    if (bucketName == null) {
        throw new CollectionException(ErrorCodes.BUCKET_NAME_EMPTY, "Bucket name is null");
    }
    if (bucketName.equals("")) {
        throw new CollectionException(ErrorCodes.BUCKET_NAME_EMPTY, "Bucket Name Empty");
    }
    if (bucketType == null) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Collection name is null");
    }
    if (bucketType.equals("")) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Collection Name Empty");
    }
    // if (!databaseService.getDbList().contains(dbName)) {
    //   throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS,
    //       "Database with dbName [ " + dbName + "] does not exist");
    // }
    MongoDatabase db = mongoInstance.getDatabase(dbName);

    GridFSBucket gridFS = GridFSBuckets.create(db, bucketName);

    // MongoCollection<Document> filesCollection =
    // getGridFSCollection(gridFS, bucketType);
    try {
        if (command.equals("find")) {
            return executeFind(gridFS, query, sortBy, limit, skip);
        } else if (command.equals("drop")) {
            return executeDrop(db, bucketName);
        } else {
            throw new InvalidMongoCommandException(ErrorCodes.COMMAND_NOT_SUPPORTED,
                    "This command is not supported in GridFS");
        }
    } catch (MongoException e) {
        throw new GridFSException(ErrorCodes.QUERY_EXECUTION_EXCEPTION, e.getMessage());
    }
}

From source file:com.imaginea.mongodb.services.impl.GridFSServiceImpl.java

License:Apache License

/**
 * Service implementation for retrieving the specified file stored in GridFS.
 *
 * @param dbName Name of Database/*from   w w w  . ja v  a 2s .  c  o m*/
 * @param bucketName Name of GridFS Bucket
 * @param _id ObjectId of the file to be retrieved
 * @return Requested multipartfile for viewing or download based on 'download' param.
 */
public File getFile(String dbName, String bucketName, String _id)
        throws ValidationException, DatabaseException, CollectionException {
    if (dbName == null) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Is Null");
    }
    if (dbName.equals("")) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty");
    }
    File tempFile = null;
    try {
        // if (!databaseService.getDbList().contains(dbName)) {
        //   throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS,

        //       "Database with dbName [ " + dbName + "] does not exist");
        // }

        ObjectId objectId = new ObjectId(_id);

        MongoDatabase db = mongoInstance.getDatabase(dbName);

        GridFSBucket gridFS = GridFSBuckets.create(db, bucketName);

        Document id = new Document();

        id.put("_id", objectId);

        GridFSFile fsFile = gridFS.find(id).first();

        if (fsFile != null) {

            String tempDir = System.getProperty("java.io.tmpdir");

            tempFile = new File(tempDir + "/" + fsFile.getFilename());

            FileOutputStream streamToDownloadTo = new FileOutputStream(tempFile);

            gridFS.downloadToStream(objectId, streamToDownloadTo);

            streamToDownloadTo.close();

        }

    } catch (MongoException m) {
        throw new CollectionException(ErrorCodes.GET_COLLECTION_LIST_EXCEPTION, m.getMessage());
    } catch (IOException e) {
        throw new CollectionException(ErrorCodes.GET_COLLECTION_LIST_EXCEPTION, e.getMessage());
    }
    return tempFile;
}

From source file:com.imaginea.mongodb.services.impl.GridFSServiceImpl.java

License:Apache License

/**
 * Service implementation for uploading a file to GridFS.
 *
 * @param dbName Name of Database//from www  .  java 2  s. c o  m
 * @param bucketName Name of GridFS Bucket
 * @param formData formDataBodyPart of the uploaded file
 * @param inputStream inputStream of the uploaded file
 * @param connectionId ConnectionId of the connection
 * @return Success message with additional file details such as name, size, download url &
 *         deletion url as JSON Array string.
 */
public JSONArray insertFile(String dbName, String bucketName, String connectionId, InputStream inputStream,
        FormDataContentDisposition fileData) throws ApplicationException {
    if (dbName == null) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database name is null");

    }
    if (dbName.equals("")) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty");
    }

    if (bucketName == null) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Bucket name is null");
    }
    if (bucketName.equals("")) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Bucket Name Empty");
    }

    JSONArray result = new JSONArray();

    try {
        // if (!databaseService.getDbList().contains(dbName)) {
        //   throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS,
        //       "DB [" + dbName + "] DOES NOT EXIST");
        // }

        MongoDatabase db = mongoInstance.getDatabase(dbName);

        GridFSBucket gridFS = GridFSBuckets.create(db, bucketName);

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

        ObjectId fileId = gridFS.uploadFromStream(fileData.getFileName(), inputStream, options);

        String objectId = JSON.serialize(fileId);
        JSONObject obj = new JSONObject();
        obj.put("name", fileData.getFileName());
        // obj.put("size", fileData.);
        obj.put("url", String.format("services/%s/%s/gridfs/getfile?id=%s&download=%s&connectionId=%s&ts=%s",
                dbName, bucketName, objectId, false, connectionId, new Date()));
        obj.put("delete_url", String.format("services/%s/%s/gridfs/dropfile?id=%s&connectionId=%s&ts=%s",
                dbName, bucketName, objectId, connectionId, new Date().getTime()));
        obj.put("delete_type", "GET");
        result.put(obj);

    } catch (MongoException e) {
        throw new CollectionException(ErrorCodes.UPLOAD_FILE_EXCEPTION, e.getMessage());
    } catch (JSONException e) {
        throw new ApplicationException(ErrorCodes.JSON_EXCEPTION, "Error creating json response obj",
                e.getCause());
    }
    return result;
}

From source file:com.imaginea.mongodb.services.impl.GridFSServiceImpl.java

License:Apache License

/**
 * Service implementation for dropping a file from GridFS.
 *
 * @param dbName Name of Database// w  ww.j  a  v  a 2 s. c o m
 * @param bucketName Name of GridFS Bucket
 * @param _id Object id of file to be deleted
 * @return Status message.
 */
public String deleteFile(String dbName, String bucketName, String _id)
        throws DatabaseException, DocumentException, CollectionException, ValidationException {
    if (dbName == null) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database name is null");

    }
    if (dbName.equals("")) {
        throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty");
    }

    if (bucketName == null) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Bucket name is null");
    }
    if (bucketName.equals("")) {
        throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Bucket Name Empty");
    }

    String result = null;
    GridFSFile gridFSFile = null;
    try {
        // if (!databaseService.getDbList().contains(dbName)) {
        //   throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS,
        //       "DB [" + dbName + "] DOES NOT EXIST");
        // }
        if (_id == null) {
            throw new DocumentException(ErrorCodes.DOCUMENT_EMPTY, "File is empty");
        }

        ObjectId objectId = new ObjectId(_id);

        MongoDatabase db = mongoInstance.getDatabase(dbName);

        GridFSBucket gridFS = GridFSBuckets.create(db, bucketName);

        Document id = new Document();

        id.put("_id", objectId);

        gridFSFile = gridFS.find(id).first();
        if (gridFSFile == null) {
            throw new DocumentException(ErrorCodes.DOCUMENT_DOES_NOT_EXIST, "Document does not exist !");
        }

        gridFS.delete(objectId);

    } catch (MongoException e) {
        throw new DocumentException(ErrorCodes.DOCUMENT_DELETION_EXCEPTION, e.getMessage());
    }
    result = "File [" + gridFSFile.getFilename() + "] has been deleted.";
    return result;
}

From source file:de.taimos.dvalin.mongo.AbstractMongoDAO.java

License:Apache License

protected GridFSBucket getGridFSBucket(String bucket) {
    if (bucket == null || bucket.isEmpty()) {
        throw new IllegalArgumentException();
    }/*from w w w . ja v  a 2 s .c om*/
    return GridFSBuckets.create(this.db, bucket);
}

From source file:io.mandrel.blob.impl.MongoBlobStore.java

License:Apache License

public MongoBlobStore(TaskContext context, MongoClient mongoClient, String databaseName, String bucketName,
        int batchSize) {
    super(context);
    this.mongoClient = mongoClient;
    this.databaseName = databaseName;
    this.bucket = GridFSBuckets.create(mongoClient.getDatabase(databaseName), bucketName);
    this.batchSize = batchSize;
    this.mapper = new ObjectMapper();
}

From source file:net.liaocy.ml4j.nlp.word2vec.Predict.java

private void load(String modelName) throws IOException {
    MongoDatabase db = Mongo.getDB();/*from w w w.j a v  a  2 s.co  m*/
    GridFSBucket gridFSBucket = GridFSBuckets.create(db, "word2vecmodels");
    File file = File.createTempFile(modelName, ".w2v");
    OutputStream os = new FileOutputStream(file);
    gridFSBucket.downloadToStreamByName(modelName, os);
    os.close();
    this.vec = WordVectorSerializer.readWord2VecModel(file);
    //        System.out.println(file.getAbsolutePath());
    if (!file.delete()) {
        file.deleteOnExit();
    }
}

From source file:net.liaocy.ml4j.nlp.word2vec.Train.java

private void saveModel(String modelName, Word2Vec vec) throws IOException {
    MongoDatabase db = Mongo.getDB();//from  w w  w . j av  a2  s  . c o m
    GridFSBucket gridFSBucket = GridFSBuckets.create(db, "word2vecmodels");

    GridFSFile gfsfi = gridFSBucket.find(new Document("filename", modelName)).first();
    if (gfsfi != null) {
        ObjectId id = gfsfi.getObjectId();
        gridFSBucket.delete(id);
    }

    try (GridFSUploadStream uploadStream = gridFSBucket.openUploadStream(modelName)) {
        WordVectorSerializer.writeWord2VecModel(vec, uploadStream);
        System.out.println("Save Model Successed!");
    }
}

From source file:net.liaocy.ml4j.tfidf.tfidf.java

public void save() throws IOException {
    MongoDatabase db = Mongo.getDB();/*www  . j av a 2 s.co m*/
    GridFSBucket gridFSBucket = GridFSBuckets.create(db, "tfidfmodels");

    GridFSFile gfsfi = gridFSBucket.find(new Document("filename", this.modelName)).first();
    if (gfsfi != null) {
        ObjectId id = gfsfi.getObjectId();
        gridFSBucket.delete(id);
    }

    for (Entry<Integer, Integer> word : wordCount.entrySet()) {
        double idf = this.getIdf(word.getValue());
        this.idfMax = Math.max(this.idfMax, idf);
        this.idfMin = Math.min(this.idfMax, idf);
    }

    try (GridFSUploadStream uploadStream = gridFSBucket.openUploadStream(this.modelName)) {
        try (ObjectOutputStream o = new ObjectOutputStream(uploadStream)) {
            o.writeObject(this.wordDocCount);
            o.writeObject(this.docCount);
            o.writeObject(this.wordCount);
            o.writeDouble(this.idfMax.doubleValue());
            o.writeDouble(this.idfMin.doubleValue());
        }
    }

    System.out.println("Save Model Successed!");
}