| = MongoDB GridFS Component |
| :doctitle: MongoDB GridFS |
| :shortname: mongodb-gridfs |
| :artifactid: camel-mongodb-gridfs |
| :description: Interact with MongoDB GridFS. |
| :since: 2.18 |
| :supportlevel: Stable |
| :tabs-sync-option: |
| :component-header: Both producer and consumer are supported |
| //Manually maintained attributes |
| :camel-spring-boot-name: mongodb-gridfs |
| |
| *Since Camel {since}* |
| |
| *{component-header}* |
| |
| Maven users will need to add the following dependency to their `pom.xml` |
| for this component: |
| |
| [source,xml] |
| ------------------------------------------------------------ |
| <dependency> |
| <groupId>org.apache.camel</groupId> |
| <artifactId>camel-mongodb-gridfs</artifactId> |
| <version>x.y.z</version> |
| <!-- use the same version as your Camel core version --> |
| </dependency> |
| ------------------------------------------------------------ |
| |
| == URI format |
| |
| ------------------------------------------------------------------------------ |
| mongodb-gridfs:connectionBean?database=databaseName&bucket=bucketName[&moreOptions...] |
| ------------------------------------------------------------------------------ |
| |
| // component-configure options: START |
| |
| // component-configure options: END |
| |
| // component options: START |
| include::partial$component-configure-options.adoc[] |
| include::partial$component-endpoint-options.adoc[] |
| // component options: END |
| |
| // endpoint options: START |
| |
| // endpoint options: END |
| // component headers: START |
| include::partial$component-endpoint-headers.adoc[] |
| // component headers: END |
| |
| == Configuration of database in Spring XML |
| |
| The following Spring XML creates a bean defining the connection to a |
| MongoDB instance. |
| |
| [source,xml] |
| ---------------------------------------------------------------------------------------------------------------------------------- |
| <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 |
| xref:mongodb-gridfs-component.adoc[*findOne*] on a collection. |
| |
| *Get a file from GridFS* |
| |
| [source,xml] |
| ---------------------------------------------------------------------------------- |
| <route> |
| <from uri="direct:start" /> |
| <!-- using bean 'mongoBean' defined above --> |
| <to uri="mongodb-gridfs:mongoBean?database=${mongodb.database}&operation=findOne" /> |
| <to uri="direct:result" /> |
| </route> |
| ---------------------------------------------------------------------------------- |
| |
| == GridFS operations - producer endpoint |
| |
| === count |
| |
| Returns the total number of file in the collection, returning an Integer |
| as the OUT message body. |
| |
| [source,java] |
| --------------------------------------------------------------------------------- |
| // from("direct:count").to("mongodb-gridfs?database=tickets&operation=count"); |
| Integer result = template.requestBodyAndHeader("direct:count", "irrelevantBody"); |
| assertTrue("Result is not of type Long", result instanceof Integer); |
| --------------------------------------------------------------------------------- |
| |
| You can provide a filename header to provide a count of files matching |
| that filename. |
| |
| [source,java] |
| ------------------------------------------------------------------------------- |
| Map<String, Object> headers = new HashMap<String, Object>(); |
| headers.put(Exchange.FILE_NAME, "filename.txt"); |
| Integer count = template.requestBodyAndHeaders("direct:count", query, headers); |
| ------------------------------------------------------------------------------- |
| |
| === listAll |
| |
| Returns an Reader that lists all the filenames and their IDs in a tab |
| separated stream. |
| |
| ---------------------------------------------------------------------------------- |
| // from("direct:listAll").to("mongodb-gridfs?database=tickets&operation=listAll"); |
| Reader result = template.requestBodyAndHeader("direct:listAll", "irrelevantBody"); |
| |
| filename1.txt 1252314321 |
| filename2.txt 2897651254 |
| ---------------------------------------------------------------------------------- |
| |
| === findOne |
| |
| Finds a file in the GridFS system and sets the body to an InputStream of |
| the content. Also provides the metadata has headers. It uses |
| Exchange.FILE_NAME from the incoming headers to determine the file to find. |
| |
| [source,java] |
| ------------------------------------------------------------------------------------------------- |
| // from("direct:findOne").to("mongodb-gridfs?database=tickets&operation=findOne"); |
| Map<String, Object> headers = new HashMap<String, Object>(); |
| headers.put(Exchange.FILE_NAME, "filename.txt"); |
| InputStream result = template.requestBodyAndHeaders("direct:findOne", "irrelevantBody", headers); |
| ------------------------------------------------------------------------------------------------- |
| |
| === create |
| |
| Creates a new file in the GridFs database. It uses the |
| Exchange.FILE_NAME from the incoming headers for the name and the body |
| contents (as an InputStream) as the content. |
| |
| [source,java] |
| ------------------------------------------------------------------------ |
| // from("direct:create").to("mongodb-gridfs?database=tickets&operation=create"); |
| Map<String, Object> headers = new HashMap<String, Object>(); |
| headers.put(Exchange.FILE_NAME, "filename.txt"); |
| InputStream stream = ... the data for the file ... |
| template.requestBodyAndHeaders("direct:create", stream, headers); |
| ------------------------------------------------------------------------ |
| |
| === remove |
| |
| Removes a file from the GridFS database. |
| |
| [source,java] |
| ------------------------------------------------------------------------ |
| // from("direct:remove").to("mongodb-gridfs?database=tickets&operation=remove"); |
| Map<String, Object> headers = new HashMap<String, Object>(); |
| headers.put(Exchange.FILE_NAME, "filename.txt"); |
| template.requestBodyAndHeaders("direct:remove", "", headers); |
| ------------------------------------------------------------------------ |
| |
| |
| |
| include::spring-boot:partial$starter.adoc[] |