//

GridFS Support in Spring Data MongoDB

26.7.2012 | 2 minutes of reading time

MongoDB

MongoDB is a highly scalable, document oriented NoSQL datastore from 10gen. For more information have a look at the MongoDB homepage: http://www.mongodb.org . A short introduction to MongoDB can be found at this blog post .

GridFS

In MongoDB the size of a single record (i.e. a JSON document) is limited to 16 MB. If you want to store and query bigger binary data than that, you have to use the GridFS API of your MongoDB driver. There is also a mongofiles command line tool in the bin folder of your MongoDB installation. We will use that for our examples. In an empty database, there are no GridFS files:

1C:\mongo\bin>mongofiles list
2connected to: 127.0.0.1
3 
4C:\mongo\bin>
5


But the first usage of GridFS leads to two new collections:

1C:\mongo\bin>mongo
2MongoDB shell version: 2.0.5
3connecting to: test
4> show collections
5fs.chunks
6fs.files
7

fs.files stores metadata, and fs.chunks holds the binary data itself. So far, these collections are empty:

1> db.fs.chunks.count()
20
3> db.fs.files.count()
40
5

Now we insert a first file by switching back to the command line and typing:

1C:\mongo\bin>mongofiles put mongo.exe
2connected to: 127.0.0.1
3added file: { _id: ObjectId('501004130b07c6ab3fb01fa3'), filename: "mongo.exe", chunkSize: 262144, 
4uploadDate: new Date(1343226899351), md5: "f5e82e7d4b7ae87a1d6e80dfc7f43468", length: 1885696 }
5done!
6 
7C:\mongo\bin>
8

Back on the mongo shell we can check our fs collections:

1E:\mongo\bin>mongo
2MongoDB shell version: 2.0.5
3connecting to: test
4> db.fs.files.count()
51
6> db.fs.chunks.count()
78
8

So uploading the mongo.exe binary produced one file that was split into 8 chunks. So much for that, for more details check out the help with

1mongofiles -help
2

GridFS Support in Spring Data MongoDB

The Spring Data MongoDB project supports access to the GridFS API since the milestone release 1.1.0.M1. In general, Spring Data is another abstraction layer on top of the more low level MongoDB Java Driver:

Let’s see how to use the GridFS support. First, we grab the latest milestone release …

1<dependency>
2   <groupId>org.springframework.data</groupId>
3   <artifactId>spring-data-mongodb</artifactId>
4   <version>1.1.0.M2</version>
5</dependency>
6

… from Spring’s snapshot repository:

1<repository>
2    <id>spring-snapshot</id>
3    <name>Spring Maven SNAPSHOT Repository</name>
4    <url>http://repo.springsource.org/libs-snapshot</url>
5</repository>
6

Spring Data MongoDB offers a GridFsTemplate to handle the GridFS operations:

1<!-- Connection to MongoDB server -->
2<mongo:db-factory host="localhost" port="27017" dbname="test" />
3<mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"/>
4 
5<!-- MongoDB GridFS Template -->
6<bean id="gridTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
7  <constructor-arg ref="mongoDbFactory"/>
8  <constructor-arg ref="converter"/>
9</bean>
10

With that template we can easily read the existing GridFS file we inserted before on the command line:

1 @Autowired GridFsTemplate template;
2 
3 @Test public void shouldListExistingFiles() {
4     List<GridFSDBFile> files = template.find(null);
5 
6     for (GridFSDBFile file: files) {
7         System.out.println(file);
8     }
9 }
10

Running the above example, you should see an output like this:

1{ "_id" : { "$oid" : "4fe9bda0f2abbef0d127a647"} , "chunkSize" : 262144 , "length" : 2418176 , 
2   "md5" : "19c2a2cc7684ce9d497a59249396ae1d" , "filename" : "mongo.exe" , "contentType" :  null  , 
3  "uploadDate" : { "$date" : "2012-06-26T13:48:16.713Z"} , "aliases" :  null }
4

In order to access the content of the file, you call GridFSDBFile#getInputStream. You can also store and delete GridFS files quite easily. Have a detailed look at the GridFsTemplate .

The full source code of the above example can be found at github.

share post

Likes

0

//

More articles in this subject area\n

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen

Wir helfen Deinem Unternehmen

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.