Skip to content

mappers

Mahmoud Ben Hassine edited this page Jun 4, 2017 · 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 easybatch-flatfile Maps delimited-values records to Java objects
FixedLengthRecordMapper StringRecord easybatch-flatfile Maps fixed-length field records to Java objects
UnivocityFixedWidthRecordMapper StringRecord easybatch-univocity Maps fixed-width records to Java objects using Univocity
ApacheCommonCsvRecordMapper StringRecord easybatch-apache-common-csv Maps delimited-values records to Java objects
OpenCsvRecordMapper StringRecord easybatch-opencsv Maps delimited-values records to Java objects using OpenCSV
UnivocityCsvRecordMapper StringRecord easybatch-univocity Maps comma separated-values records to Java objects using Univocity
UnivocityTsvRecordMapper StringRecord easybatch-univocity Maps tab separated-values records to Java objects using Univocity
XmlRecordMapper XmlRecord easybatch-xml Maps xml records to Java objects annotated with JAXB annotations
XstreamRecordMapper XmlRecord easybatch-xstream Maps xml records to Java objects using XStream
JacksonRecordMapper JsonRecord easybatch-jackson Maps json records to Java objects using Jackson
GsonRecordMapper JsonRecord easybatch-gson Maps json records to Java objects using Gson
YamlRecordMapper YamlRecord easybatch-yaml Maps yaml records to Java objects using yamlbeans
JdbcRecordMapper JdbcRecord easybatch-jdbc Maps JDBC records to Java objects
SpringJdbcRecordMapper JdbcRecord easybatch-spring Maps json records to Java objects using Spring JDBC
MongoDBRecordMapper MongoDBRecord easybatch-mongodb Maps mongodb records to Java objects using Morphia
MsExcelRecordMapper MsExcelRecord easybatch-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)](http://www.ietf.org/rfc/rfc4180.txt[).
  • 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();

Json to object mapping

Easy Batch does not provide support for JSON mapping yet. The goal is to use the reference implementation of JSON-B. As of version 5.0.0, the reference implementation of JSON-B is not available yet. Built-in support for JSON mapping will be provided as soon as the reference implementation of JSON-B will become available. You can still use easybatch-jackson and easybatch-gson modules which provide JSON mapping using third party libraries.

Clone this wiki locally