Skip to content

mappers

Mahmoud Ben Hassine edited this page Feb 7, 2020 · 7 revisions

Easy Batch development is POJO-centric and has been designed with the idea that input records should be projected in the object oriented world and not used in their original raw format.

It is the RecordMapper that lets you map input record payloads to domain objects. You can register an implementation of the RecordMapper interface as follows:

Job job = new JobBuilder()
    .mapper(new MyRecordMapper())
    .build();

There are several built-in record mappers to map records from a variety of data formats:

Record mapper Expected record type Module Description
DelimitedRecordMapper StringRecord easy-batch-flatfile Maps delimited-values records to Java objects
FixedLengthRecordMapper StringRecord easy-batch-flatfile Maps fixed-length field records to Java objects
UnivocityFixedWidthRecordMapper StringRecord easy-batch-univocity Maps fixed-width records to Java objects using Univocity
ApacheCommonCsvRecordMapper StringRecord easy-batch-apache-common-csv Maps delimited-values records to Java objects
OpenCsvRecordMapper StringRecord easy-batch-opencsv Maps delimited-values records to Java objects using OpenCSV
UnivocityCsvRecordMapper StringRecord easy-batch-univocity Maps comma separated-values records to Java objects using Univocity
UnivocityTsvRecordMapper StringRecord easy-batch-univocity Maps tab separated-values records to Java objects using Univocity
XmlRecordMapper XmlRecord easy-batch-xml Maps xml records to Java objects annotated with JAXB annotations
JsonRecordMapper JsonRecord easy-batch-json Maps json records to Java objects using JsonB
XstreamRecordMapper XmlRecord easy-batch-xstream Maps xml records to Java objects using XStream
JacksonRecordMapper JsonRecord easy-batch-jackson Maps json records to Java objects using Jackson
GsonRecordMapper JsonRecord easy-batch-gson Maps json records to Java objects using Gson
YamlRecordMapper YamlRecord easy-batch-yaml Maps yaml records to Java objects using yamlbeans
JdbcRecordMapper JdbcRecord easy-batch-jdbc Maps JDBC records to Java objects
SpringJdbcRecordMapper JdbcRecord easy-batch-spring Maps json records to Java objects using Spring JDBC
MsExcelRecordMapper MsExcelRecord easy-batch-msexcel Maps MsExcel records to Java objects using Apache POI

Record mappers take a Record, map it's payload to a domain object and return another Record with the same header but with the domain object as payload.

Custom type conversion

Some record mappers ( DelimitedRecordMapper, FixedLengthRecordMapper, ApacheCommonCsvRecordMapper and JdbcRecordMapper) convert raw textual data to typed data in Java objects. Easy Batch supports all Java primitive and wrapper types. If you want to provide a custom type converter, you can implement the org.easybatch.core.api.TypeConverter interface and register your implementation within the record mapper in use.

Delimited-values records considerations

If a delimited record is not well formed, the DelimitedRecordMapper throws an exception that causes the record to be rejected in the following cases:

  • Fields number is not equal to the expected fields number (missing field, extra field, etc) as specified in the CSV RFC section 2.4.
  • A field is not qualified as expected with the data qualifier, which means that the DelimitedRecordMapper expects all fields to be qualified when a qualifier is specified.

DelimitedRecordMapper limitations

The DelimitedRecordMapper is intended to cover basic requirements of delimited-values mapping. It does not support detecting delimiters and line breaks in a qualified field. If you need these features, you can use the ApacheCommonCsvRecordMapper or the OpenCsvRecordMapper.

Fixed-length records considerations

If a fixed length record is not well formed, the FixedLengthRecordMapper throws an exception that causes the record to be rejected. A fixed length record is not well formed if its length is not equal to expected record length.

Xml record validation

When you create a XmlRecordMapper, you should specify your target domain object type. If you need to validate Xml records against a Xsd schema, you can specify the schema at creation time as follows:

Job job = new JobBuilder()
        .mapper(new XmlRecordMapper(MyPojoType.class, myXsdFile))
        .build();