Robert Varga
Robert Varga

Reputation: 341

How to parse CSV(with values containing commas) with Spring Batch

I would like to parse a line like this:

"A", "aa,bb", ,"aa"

into these tokens:

A | aa,bb | (empty string) | aa

How would the FileItemReader definition look like? I guess I will need my own DelimitedLineTokenizer?

Thanks

Upvotes: 1

Views: 5181

Answers (3)

Jishnu Kumaran
Jishnu Kumaran

Reputation: 1

Even I had the same scenario where one text containing a comma was slipping to a new cell. I changed my XML and added a CustomDelimitedLineAggregator.

eg:) My XML will be having below code:-

    <property name="lineAggregator">
        <bean
            class="com.CustomDelimitedLineAggregator">
            <property name="delimiter" value="|" />
            <property name="fieldExtractor">
                <bean
                    class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                    <property name="names"
                        value="entityHashKey,entityLocationClli,entityLocationName,entityLocationType,entitySCISCode,entityStateCode,entityCountryCode,entityRegion" />
                </bean>
            </property>
        </bean>
    </property>
</bean>

Also created the custom class (i.e. CustomDelimitedLineAggregator.java) like below:-

package com;

import org.springframework.batch.item.file.transform.ExtractorLineAggregator; import org.springframework.util.StringUtils;

public class CustomDelimitedLineAggregator extends ExtractorLineAggregator { private String delimiter;

public CustomDelimitedLineAggregator() {
    this.delimiter = ",";
}

public void setDelimiter(String delimiter) {
    this.delimiter = delimiter;
}

@Override
public String doAggregate(Object[] fields) {

    String arrregateVal = StringUtils.arrayToDelimitedString(fields, this.delimiter);
    if(null != arrregateVal && arrregateVal.contains(","))
    {
        arrregateVal = "\""+arrregateVal+"\"";
    }
    return arrregateVal;
}

}

Here in the doAggregate method, each row's full data is coming and if any row contains the comma it will put the entire row inside (i.e. double quote) " My full row data which contains a comma " Hence the entire line will come inside a single row even if contains a comma in this.

Upvotes: 0

user3222372
user3222372

Reputation: 382

DelimitedLineTokenizer should work to parse a comma or pipe. If you are thinking to read a file which is comma separated and convert into pipe separated, you need to enrich your item (in processor) and then persist it.

Upvotes: 1

Dayanand Kadam
Dayanand Kadam

Reputation: 21

Check this example http://www.mkyong.com/spring-batch/spring-batch-hello-world-example/

If comma is going to be a delimiter then you need not create your own DelimitedLineTokenizer. You can use "org.springframework.batch.item.file.transform.DelimitedLineTokenizer"

Upvotes: 1

Related Questions