Using GridFS within Scala to store large files in MongoDB


GridFS is a specification for storing large files in MongoDB. Some of the features of GridFS are :

* GridFS tends to handle large numbers of files better than many file systems.
* Query Support is good.
* If you have certain files that change a lot then it makes sense to store them in GridFS so you can modify them in one place and all clients will get the updates.
Find more features..

Here we’ll look at how to store the large files in GridFS. Let us elaborate it via taking an example of storing an image in to GridFS.

1. We need to have a connection with MongoDB and we can create this connection as :

val mongoDBConnection = MongoConnection()("myTestDB")

2. Now we need to create a GridFS that’ll store the files , within this mongo connection.

 val gridFS = GridFS(mongoDBConnection)

3. Create the java inputstream from the file that we want to store in GridFS.

val imageFile = new File("/home/neelkanth/neel.jpg")
val fileInputStream=new FileInputStream(imageFile)

4. We can now call the createFile method of GridFS to store the file. We can specify the
attributes of that file as well.

  val gfsFile=gridFS.createFile(fileInputStream)
  gfsFile.filename="Neel.jpg"
  gfsFile.save

Here’s the complete code file :

package tester

import com.mongodb.casbah.MongoConnection
import java.io.File
import java.io.FileInputStream
import com.mongodb.casbah.gridfs.GridFS

object GridFSTest extends App {
  
  /*
   * Making The Connection
   */
  
    val mongoDBConnection = MongoConnection()("myTestDB")
    
    
      /*
     * Initiating the GridFS within the created Mongo Connection
     */
    
    val gridFS = GridFS(mongoDBConnection)
    
    /*
     * Creating the inputstream of java file
     */
    
    val imageFile = new File("/home/neelkanth/neel.jpg")
    val fileInputStream=new FileInputStream(imageFile)
    
  
    /*
     * Saving the file in to GridFS
     */
    
    val gfsFile=gridFS.createFile(fileInputStream)
    gfsFile.filename="Neel.jpg"
    gfsFile.save
    
    println("Your file has been successfully saved")
    
    

}
Advertisements

About Neelkanth Sachdeva

Project Manager @ eCIFM Solutions Inc. Previous : Senior Software Engineer @ Evernym Inc. Previous : Software Consultant @ Knoldus Software LLP Software Consultant @ Inphina Technologies
This entry was posted in Cloud, Scala, Web and tagged , , . Bookmark the permalink.

3 Responses to Using GridFS within Scala to store large files in MongoDB

  1. thomas says:

    Thank you for this tutorial, but unfortunately it does not cover getting files out of mongodb gridfs using casbah. I haven’t found a tutorial that shows this process. The official documentation tells you to use findOne(filename). That’s fine and you get your Gridfsdbfile. But what i wasn’t able to figure out is, how to get the data from the different chunks. If I access the inputStream of the gridfsdbfile i just get the data from one chunk.
    I’d be really happy if you could add some more information on this topic.
    Thank you in advance
    Thomas

  2. CK says:

    Yes, please continue this excellent example. To show how to manipulate the retrieved file would be excellent.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s