uniVocity TSV
Since Camel 2.15
This Data Format uses uniVocity-parsers for reading and writing three kinds of tabular data text files:
-
CSV (Comma Separated Values), where the values are separated by a symbol (usually a comma)
-
fixed-width, where the values have known sizes
-
TSV (Tabular Separated Values), where the fields are separated by a tabulation
Thus, there are three data formats based on uniVocity-parsers.
If you use Maven, you can add the following to your pom.xml, substituting the version number for the latest and greatest release.
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-univocity-parsers</artifactId>
<version>x.x.x</version>
</dependency> Options
Most configuration options of the uniVocity-parsers are available in the data formats. If you want more information about a particular option, please refer to their documentation page.
The three data formats share common options and have dedicated ones, this section presents them all.
Options
The uniVocity TSV dataformat supports the following options which are listed below.
| Name | Default | Java Type | Description |
|---|---|---|---|
|
| The escape character. | |
| The string representation of a null value. | ||
|
| Whether or not the empty lines must be ignored. | |
|
| Whether or not the trailing white spaces must be ignored. | |
|
| Whether or not the leading white spaces must be ignored. | |
|
| Whether or not the headers are disabled. When defined, this option explicitly sets the headers as null which indicates that there is no header. | |
|
| Whether or not the header must be read in the first line of the test document. | |
| The maximum number of record to read. | ||
| The String representation of an empty value. | ||
| The line separator of the files. The default value is to use the JVM platform line separator. | ||
| The normalized line separator of the files. The default value is a new line character. | ||
|
| The comment symbol. | |
|
| Whether the unmarshalling should produce an iterator that reads the lines on the fly or if all the lines must be read at once. | |
|
| Whether the unmarshalling should produce maps for the lines values instead of lists. It requires to have header (either defined or collected). |
Message Body
The message body at each side of the uniVocity TSV data format: Marshal input and Unmarshal input are the bodies marshal and unmarshal can take, and Marshal output and Unmarshal output the body after them.
| Side | Body | Description |
|---|---|---|
Marshal input |
| One row as a Map, or the rows as a List (or an Iterator) of Maps. Rows that are Lists, as unmarshal gives without asMap, are not accepted. |
Marshal output |
| The TSV in the stream cache, or a byte array when stream caching is off. |
Unmarshal input |
| |
Unmarshal output |
| A List of the rows, each a List of its values (the first line too, unless headerExtractionEnabled); with asMap, a Map of the header names and the values.
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers are described in the Camel Catalog.
Marshalling usages
The marshalling accepts either:
-
A list of maps (
List<Map<String, ?>>), one for each line -
A single map (
Map<String, ?>), for a single line
Any other body will throws an exception.
Usage example: marshalling a Map into CSV format
-
Java
-
XML
-
YAML
from("direct:input")
.marshal().univocityCsv()
.to("mock:result"); <route>
<from uri="direct:input"/>
<marshal>
<univocityCsv/>
</marshal>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:input
steps:
- marshal:
univocityCsv: {}
- to:
uri: mock:result Usage example: marshalling a Map into fixed-width format
-
Java
-
XML
-
YAML
from("direct:input")
.marshal().univocityFixed()
.to("mock:result"); <route>
<from uri="direct:input"/>
<marshal>
<univocityFixed padding="_">
<univocityHeader length="5"/>
<univocityHeader length="5"/>
<univocityHeader length="5"/>
</univocityFixed>
</marshal>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:input
steps:
- marshal:
univocityFixed:
padding: "_"
- to:
uri: mock:result Usage example: marshalling a Map into TSV format
-
Java
-
XML
-
YAML
from("direct:input")
.marshal().univocityTsv()
.to("mock:result"); <route>
<from uri="direct:input"/>
<marshal>
<univocityTsv/>
</marshal>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:input
steps:
- marshal:
univocityTsv: {}
- to:
uri: mock:result Unmarshalling usages
The unmarshalling uses an InputStream in order to read the data.
Each row produces either:
-
a list with all the values in it (
asMapoption withfalse); -
A map with all the values indexed by the headers (
asMapoption withtrue).
All the rows can either:
-
be collected at once into a list (
lazyLoadoption withfalse); -
be read on the fly using an iterator (
lazyLoadoption withtrue).
Usage example: unmarshalling a CSV format into maps with automatic headers
-
Java
-
XML
-
YAML
from("direct:input")
.unmarshal().univocityCsv()
.to("mock:result"); <route>
<from uri="direct:input"/>
<unmarshal>
<univocityCsv headerExtractionEnabled="true" asMap="true"/>
</unmarshal>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:input
steps:
- unmarshal:
univocityCsv:
headerExtractionEnabled: true
asMap: true
- to:
uri: mock:result Usage example: unmarshalling a fixed-width format into lists
-
Java
-
XML
-
YAML
from("direct:input")
.unmarshal().univocityFixed()
.to("mock:result"); <route>
<from uri="direct:input"/>
<unmarshal>
<univocityFixed>
<univocityHeader length="5"/>
<univocityHeader length="5"/>
<univocityHeader length="5"/>
</univocityFixed>
</unmarshal>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:input
steps:
- unmarshal:
univocityFixed: {}
- to:
uri: mock:result