简体   繁体   中英

How to access read raw data in a spring batch job (xml or csv)

I have different import jobs to get data into our system. Since the data is coming from different producers, i sometimes have csv data, or different formats of xml. Each of these import types has its own spring batch job with its own configuration (different readers, different processors but all the same writer).

My task now is, that i also need the "raw data" in my system and not only the converted objects. So for a csv import i want to have access to the raw line that builds up one entity. In XML i want the raw element as string. So the writer should be extended to take my converted object DTO plus an additional string with the raw data that was read.

I just can´t figure out how to access the raw data with spring batch. I tried several ways to get into the processing line with ItemReadListener#beforeRead or afterRead but i cannot access the raw data from the files.

Any ideas for what i can look further? Or tips on how to achive getting the raw data + the converted dto objects?

You do not have to map data to a domain objects, your items can be of type String .

For flat files, you can use the PassThroughLineMapper which will give you the raw line verbatim:

@Bean
public FlatFileItemReader<String> itemReader() {
    return new FlatFileItemReaderBuilder<String>()
            .name("rawDataReader")
            .resource(new FileSystemResource("/absolute/path/to/your/flat/file"))
            .lineMapper(new PassThroughLineMapper())
            .build();
}

For XML files, you can use the same approach with a StaxEventItemReader<String> . However, Spring Batch delegates the unmarshalling process to org.springframework.oxm.Marshaller , so depending on which XML implementation you use, you need to configure the unmarshaller accordingly:

@Bean
public StaxEventItemReader<String> itemReader() {
    Marshaller marshaller = .. // create or inject marshaller
    // configure marshaller to unmarshal raw strings
    return new StaxEventItemReaderBuilder<String>()
            .name("rawXmlDataReader")
            .resource(new FileSystemResource("/absolute/path/to/your/xml/file"))
            .addFragmentRootElements("yourTagName")
            .unmarshaller(marshaller)
            .build();
}

For Jaxb, this might help: JAXB use String as it is

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM