List of usage examples for com.mongodb.client.gridfs GridFSBucket openUploadStream
GridFSUploadStream openUploadStream(String filename);
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();// ww w .ja v a 2s .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: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 2s.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:net.liaocy.ml4j.nlp.word2vec.Train.java
private void saveModel(String modelName, Word2Vec vec) throws IOException { MongoDatabase db = Mongo.getDB();/*from w ww . ja v a 2 s . co 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();//from ww w .j a v a2 s . c o 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!"); }