Skip to content
Advertisement

Spring batch filtering data inside item reader

I’m writing a batch that reads log files which should take many types (format of log log file ) then I want to read every file based on some characters inside log files for example

15:31:44,437 INFO <NioProcessor-32> Send to <SLE- 15:31:44,437 INFO <NioProcessor-32> [{2704=5, 604=1, {0=023pdu88mW00007z}] 15:31:44,437 DEBUG <NioProcessor-32> SCRecord 2944

In such a log file I want to read only log lines which contain ‘ [{}] ‘ and ignore all others. I have tried to read it in item reader and split it to object but I can’t figure how. I think that I should create a custom item reader or something like that; my Logline class looks too simple:

public class logLine {

String idOrder;

String time;

String Tags;

}

and my item reader look like:

public FlatFileItemReader<logLine> customerItemReader() {
        FlatFileItemReader<logLine> reader = new FlatFileItemReader<>();

        reader.setResource(new ClassPathResource("/data/customer.log"));

        DefaultLineMapper<LogLine> customerLineMapper = new DefaultLineMapper<>();

        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setNames(new String[] {"idOrder", "date", "tags"});

        customerLineMapper.setLineTokenizer(tokenizer);
        customerLineMapper.setFieldSetMapper(new CustomerFieldSetMapper());    
        reader.setLineMapper(customerLineMapper);

        return reader;
    }

How can I add a filter in this item reader to read only lines which contain [{

without doing the job in the item processor

Advertisement

Answer

filtering should be responsibility of processor and not the reader. You can use composite item processor and add First processor as Filtering.

Filtering processor should return null for log lines which does not contain ‘ [{}] ‘ .

These rows will be automatically ignore in next processor and in writer.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement