Camel GridFS component
Available as of Camel 2.17
Maven users will need to add the following dependency to their pom.xml
for this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-gridfs</artifactId> <version>x.y.z</version> <!-- use the same version as your Camel core version --> </dependency>
URI format
gridfs:connectionBean?database=databaseName&bucket=bucketName[&moreOptions...]
Endpoint options
GridFS endpoints support the following options, depending on whether they are acting like a Producer or as a Consumer (options vary based on the consumer type too).
Name | Default Value | Description | Producer | Consumer |
---|---|---|---|---|
| none | Required. The name of the database to which this endpoint will be bound. All operations will be executed against this database. | ||
| fs | The name of the GridFS bucket within the Database. The default is the GridFS.DEFAULT_BUCKET value ("fs"). | ||
| create | The id of the operation this endpoint will execute. Pick from the following:
|
|
Configuration of database in Spring XML
The following Spring XML creates a bean defining the connection to a MongoDB instance.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mongoBean" class="com.mongodb.Mongo"> <constructor-arg name="host" value="${mongodb.host}" /> <constructor-arg name="port" value="${mongodb.port}" /> </bean> </beans>
Sample route
The following route defined in Spring XML executes the operation findOne on a collection.
<route> <from uri="direct:start" /> <!-- using bean 'mongoBean' defined above --> <to uri="gridfs:mongoBean?database=${mongodb.database}&operation=findOne" /> <to uri="direct:result" /> </route>
GridFS operations - producer endpoints
Query operations
count
Returns the total number of file in the collection, returning a Long as the OUT message body.
// from("direct:count").to("gridfs?database=tickets&operation=count"); Long result = template.requestBodyAndHeader("direct:count", "irrelevantBody"); assertTrue("Result is not of type Long", result instanceof Long);
You can provide a filename header to provide a count of files matching that filename.
Map<String, Object> headers = new HashMap<String, Object>(); headers.put(Exchange.FILE_NAME, "filename.txt"); Long count = template.requestBodyAndHeaders("direct:count", query, headers);
GridFS Consumer
See also
- MongoDB website
- NoSQL Wikipedia article
- MongoDB Java driver API docs - current version
- Unit tests for more examples of usage