[JAVA] [Apache Camel] Use File component

What is the File component?

The File component is a component for accessing the file system and can input and output files and directories.

Consumer/Producer

The File component plays the role of both Consumer / Producer. As a Consumer, you can read files and directories and move / delete read files. As a Producer, you can write to files, etc.

With Consumer / Producer, you can achieve the following as an example.

Consumer --Read a file that exists in the specified directory --Read a file with the specified file path --Delete the file with the specified file path --Read a file that matches the conditions

Producer --Create a file in the specified path --Creates a file in the specified path and creates a file with [filename] .done when the creation is completed.

Component URI

The URI for using the File component looks like this:

file:directoryName

In the context path (directoryName), specify the directory to be processed. The File component accesses and processes the file that exists in the specified directoryName. Various processing can be performed by specifying options for this context path. There are many options for the File component, and a description of each option can be found on the official website below.

https://github.com/apache/camel/blob/master/components/camel-file/src/main/docs/file-component.adoc#uri-options

In this article as well, I would like to explain some options by showing an implementation example.

Implementation example

In this section, we will show an implementation example of the File component using the root described in Java DSL. The list is limited to frequently used options, and the options listed are only an overview, so please refer to the description of the File component on the official website above for details.

Example) Copy the files in the specified directory

The root to copy the files in the specified directory. Copy all the files in the data / input directory to data / output. Specifying the noop = true option makes it read-only and does not move or delete files in the data / input directory. You can also automatically enable the idempotent = true option by specifying the noop = true option, which will prevent the same file from being processed.

from("file:data/input?noop=true").to("file:data/output");

Example) Move files in the specified directory

The root to move files in the specified directory. Move all files in the data / input directory to data / output. delete = true will delete the file after processing.

from("file:data/input?delete=true").to("file:data/output");

Example) Recursively move files in the specified directory

A root that recursively moves files in the specified directory, including files that exist in subdirectories. The subdirectories themselves are not moved. It is specified to process recursively by recursive = true.

from("file:data/input?delete=true&recursive=true").to("file:data/output");

Example) Create a file called .done after the process is completed.

This is the root that copies the file in the specified directory and creates a file called [filename] .done after the process is completed. When all the files have been copied to the / data / output directory, a [filename] .done file will be created in the destination directory. $ {file: name} represents the name of the file being processed.

from("file:data/input?noop=true").to("file:data/output?doneFileName=${file:name}.done");

Example) If a file named [filename] .done exists, move the file.

On the contrary, if the file [filename] .done exists, the file will be moved.

from("file:data/output?noop=true&doneFileName=${file:name}.done").to("file:data/output.done");

Example) Get the list of file names and the contents of the text file.

Reads the file in the data / input directory, gets the list of file names and the contents of the text file, and outputs it. The FileListProcessor processor is specified in the process method.

from("file:data/input?noop=true").process(new FileListProcessor());

The FileListProcessor processor has the following contents.

FileListProcessor.java


package example.camelbegginer.file;

import java.util.Map;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class FileListProcessor implements Processor {

	@Override
	public void process(Exchange exchange) throws Exception {
		String body = exchange.getIn().getBody(String.class);
		System.out.println("body: " + body);

		Map<String, Object> headers = exchange.getIn().getHeaders();
		System.out.println("fileName: " + headers.get("CamelFileAbsolutePath"));

	}
}

In the process method, the contents of the file are output to the standard output with the following code.

		String body = exchange.getIn().getBody(String.class);
		System.out.println("body: " + body);

File information such as filenames goes into the message header. The header of the message is acquired as shown below, the file path is acquired by "CamelFileAbsolutePath", and it is output to the standard output.

		Map<String, Object> headers = exchange.getIn().getHeaders();
		System.out.println("fileName: " + headers.get("CamelFileAbsolutePath"));

Please refer to the following page for the value to be entered in the header of the message. https://github.com/apache/camel/blob/master/components/camel-file/src/main/docs/file-component.adoc#message-headers

In addition to the file path, you can get information such as file size and last update date.

Example) Process only files that include the specified character string in the file name

This route processes only files that include the specified character string in the file name. Use the filterFile option and write as follows.

from("file:data/input?noop=true&filterFile=$simple{file:name} contains 'file1'")
						.to("file:data/output");

filterFile=$simple{file:name} contains 'file1'

The value of the filterFile option above represents the case where the file name contains the string "file1". The value of this filterFile option is specified using "Simple Expression Language" and File Language.

The Simple expression language is Apache Camel's own expression language, which is used to output the results of calculations and value references, and is written as $ simple {variable}. Most calculations and value references can be achieved with the Simple language. File Language is an extension of the Simple expression language that allows you to refer to file-related values.

In the previous example, "file: name" represents a file name reference in File Language, and "$ simple {file: name} contains'file1'" is entirely in Simple language, with the letters "file1" in the file name. It will represent the case including columns.

For details on Simple language and File Language, refer to the official website below.

Example) Compress the specified file to zip

It's a bit off the File component, but to give you an idea of how useful Apache Camel is, let's talk about file compression in combination with the zip component. If you want to zip the file file1.txt, you can easily do it by just the following root.

					from("file:data/input?noop=true&fileName=file1.txt")
						.marshal().zipFile()
						.to("file:data/output");

In addition, in order to use the above "zipFile ()", it is necessary to add the following dependencies.

pom.xml


		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-zipfile</artifactId>
			<version>${camel.version}</version>
		</dependency>

reference

-File Component (Apache Camel official) -Simple Expression Language This is the official Camel manual for the Simple language. -File Language (Apache Camel official) The official Camel manual for File Language.

Recommended Posts

[Apache Camel] Use File component
Use only for writing files --Apache Camel
What's new in Apache Camel 2.19.0
How to use Apache POI
Apache Camel in the cloud-native era
Circuit Breaker Pattern with Apache Camel