Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read complete line without any mapper with spring batch

I am learning spring batch, i have basic idea of reader, mapper and writer. Now my current requirement is to read each line from file, compress the data and write in a gigaspace based in-memory grid.

I understand that a line mapper is compulsory attribute for iteam reader. I have no use of created a mapper, all i need to do is read the line and send it to writer to write in the grid. So how can i skip the line mapper or how can i read the plain line. Currently i have done something like this, which doesnot seems to be idead solution.

    public class ShrFileMapper implements FieldSetMapper<SpaceDocument> {
    @Override
    public SpaceDocument mapFieldSet(FieldSet fieldSet) throws BindException {
        String positionId = fieldSet.readString(0);
        StringBuffer line = new StringBuffer();
        for (String fieldValue : fieldSet.getValues()) {
            line.append("\t").append(fieldSet);
        }

        // logic for compression

        SpaceDocument spaceDocument = new SpaceDocument("shr.doc");
        spaceDocument.setProperty("id", positionId);
        spaceDocument.setProperty("payload", compressedString);

        return spaceDocument;
    }
}
like image 997
Ambuj Jauhari Avatar asked Dec 19 '22 14:12

Ambuj Jauhari


1 Answers

Assuming you are using a FlatFileItemReader, you need to provide a resource and a LineMapper. As you do not want to turn the line of input into anything else you do not need a LineTokenizer, you just want to passthrough the raw input. For more information you can checkout the official documentation:

http://docs.spring.io/spring-batch/reference/html/readersAndWriters.html#flatFileItemReader

Spring has provided already such functionality. Please checkout the PassThroughLineMapper https://github.com/spring-projects/spring-batch/blob/master/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/PassThroughLineMapper.java

public class PassThroughLineMapper implements LineMapper<String>{

    @Override
    public String mapLine(String line, int lineNumber) throws Exception {
        return line;
    }

}

This class does exactly what you need!

like image 199
Sander_M Avatar answered Apr 07 '23 13:04

Sander_M