Search in sources :

Example 1 with GridFSBucket

use of com.mongodb.reactivestreams.client.gridfs.GridFSBucket in project mongo-java-driver by mongodb.

the class GridFSTour method main.

/**
 * 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
 */
public static void main(final String[] args) throws FileNotFoundException, IOException {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = MongoClients.create();
    } else {
        mongoClient = MongoClients.create(args[0]);
    }
    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");
    ObservableSubscriber<Void> dropSubscriber = new OperationSubscriber<>();
    database.drop().subscribe(dropSubscriber);
    dropSubscriber.await();
    GridFSBucket gridFSBucket = GridFSBuckets.create(database);
    /*
         * UploadFromPublisher Example
         */
    // Get the input publisher
    Publisher<ByteBuffer> publisherToUploadFrom = toPublisher(ByteBuffer.wrap("MongoDB Tutorial..".getBytes(StandardCharsets.UTF_8)));
    // Create some custom options
    GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024).metadata(new Document("type", "presentation"));
    ObservableSubscriber<ObjectId> uploadSubscriber = new OperationSubscriber<>();
    gridFSBucket.uploadFromPublisher("mongodb-tutorial", publisherToUploadFrom, options).subscribe(uploadSubscriber);
    ObjectId fileId = uploadSubscriber.get().get(0);
    /*
         * Find documents
         */
    System.out.println("File names:");
    ConsumerSubscriber<GridFSFile> filesSubscriber = new ConsumerSubscriber<>(gridFSFile -> System.out.println(" - " + gridFSFile.getFilename()));
    gridFSBucket.find().subscribe(filesSubscriber);
    filesSubscriber.await();
    /*
         * Find documents with a filter
         */
    filesSubscriber = new ConsumerSubscriber<>(gridFSFile -> System.out.println("Found: " + gridFSFile.getFilename()));
    gridFSBucket.find(eq("metadata.contentType", "image/png")).subscribe(filesSubscriber);
    filesSubscriber.await();
    /*
         * DownloadToPublisher
         */
    ObservableSubscriber<ByteBuffer> downloadSubscriber = new OperationSubscriber<>();
    gridFSBucket.downloadToPublisher(fileId).subscribe(downloadSubscriber);
    Integer size = downloadSubscriber.get().stream().map(Buffer::limit).reduce(0, Integer::sum);
    System.out.println("downloaded file sized: " + size);
    /*
         * DownloadToStreamByName
         */
    GridFSDownloadOptions downloadOptions = new GridFSDownloadOptions().revision(0);
    downloadSubscriber = new OperationSubscriber<>();
    gridFSBucket.downloadToPublisher("mongodb-tutorial", downloadOptions).subscribe(downloadSubscriber);
    size = downloadSubscriber.get().stream().map(Buffer::limit).reduce(0, Integer::sum);
    System.out.println("downloaded file sized: " + size);
    /*
         * Rename
         */
    OperationSubscriber<Void> successSubscriber = new OperationSubscriber<>();
    gridFSBucket.rename(fileId, "mongodbTutorial").subscribe(successSubscriber);
    successSubscriber.await();
    System.out.println("Renamed file");
    /*
         * Delete
         */
    successSubscriber = new OperationSubscriber<>();
    gridFSBucket.delete(fileId).subscribe(successSubscriber);
    successSubscriber.await();
    System.out.println("Deleted file");
    // Final cleanup
    successSubscriber = new OperationSubscriber<>();
    database.drop().subscribe(successSubscriber);
    successSubscriber.await();
    System.out.println("Finished");
}
Also used : Document(org.bson.Document) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Publisher(org.reactivestreams.Publisher) ConsumerSubscriber(reactivestreams.helpers.SubscriberHelpers.ConsumerSubscriber) OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) MongoClients(com.mongodb.reactivestreams.client.MongoClients) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) MongoClient(com.mongodb.reactivestreams.client.MongoClient) GridFSUploadOptions(com.mongodb.client.gridfs.model.GridFSUploadOptions) GridFSBuckets(com.mongodb.reactivestreams.client.gridfs.GridFSBuckets) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) GridFSDownloadOptions(com.mongodb.client.gridfs.model.GridFSDownloadOptions) GridFSBucket(com.mongodb.reactivestreams.client.gridfs.GridFSBucket) ObservableSubscriber(reactivestreams.helpers.SubscriberHelpers.ObservableSubscriber) Buffer(java.nio.Buffer) PublisherHelpers.toPublisher(reactivestreams.helpers.PublisherHelpers.toPublisher) ObjectId(org.bson.types.ObjectId) Filters.eq(com.mongodb.client.model.Filters.eq) ByteBuffer(java.nio.ByteBuffer) Buffer(java.nio.Buffer) ObjectId(org.bson.types.ObjectId) OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Document(org.bson.Document) ByteBuffer(java.nio.ByteBuffer) GridFSBucket(com.mongodb.reactivestreams.client.gridfs.GridFSBucket) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ConsumerSubscriber(reactivestreams.helpers.SubscriberHelpers.ConsumerSubscriber) GridFSDownloadOptions(com.mongodb.client.gridfs.model.GridFSDownloadOptions) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) GridFSUploadOptions(com.mongodb.client.gridfs.model.GridFSUploadOptions)

Aggregations

GridFSDownloadOptions (com.mongodb.client.gridfs.model.GridFSDownloadOptions)1 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)1 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)1 Filters.eq (com.mongodb.client.model.Filters.eq)1 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 MongoClients (com.mongodb.reactivestreams.client.MongoClients)1 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)1 GridFSBucket (com.mongodb.reactivestreams.client.gridfs.GridFSBucket)1 GridFSBuckets (com.mongodb.reactivestreams.client.gridfs.GridFSBuckets)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Buffer (java.nio.Buffer)1 ByteBuffer (java.nio.ByteBuffer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Document (org.bson.Document)1 ObjectId (org.bson.types.ObjectId)1 Publisher (org.reactivestreams.Publisher)1 PublisherHelpers.toPublisher (reactivestreams.helpers.PublisherHelpers.toPublisher)1 ConsumerSubscriber (reactivestreams.helpers.SubscriberHelpers.ConsumerSubscriber)1 ObservableSubscriber (reactivestreams.helpers.SubscriberHelpers.ObservableSubscriber)1