j-money
j-money

Reputation: 529

Apache camel split csv line by line

I am trying to do a few transformations on a .csv file. My current code is

CsvDataFormat csv = new CsvDataFormat();

    from("file:/pathToFile")
    .unmarshal(csv)
    .convertBodyTo(List.class)
    .process(new CsvParserProcess())
    .marshal(csv)
    .to("file:/pathToOut").log("Finished Transformation").end();

which works however I think this loads the entire file into memory (?) and I want to split it up line by line for now so I tried

CsvDataFormat csv = new CsvDataFormat();

    from("file:/pathToFile")
    .unmarshal(csv)
    .split(body().tokenize("/n")).streaming()
    .process(new CsvParserProcess())
    .marshal(csv)
    .to("file:/pathToOut").log("Finished Transformation").end();

however I get the error No type converter available to convert from type: java.lang.String to the required type: java.util.List with value ... with the accompanying processor class

public void process(Exchange exchange) throws Exception {
    System.out.println(exchange.getIn().getBody());
}

which prints out

[[data0,data1, .... , dataN], [data0,....,dataN],...,[data0,...,dataN]]

after the stacktrace. I don't really even see why I would be getting a type conversion? And what I am doing wrong...

Edit: I forgot a few brackets in my output of sysout

Upvotes: 1

Views: 3773

Answers (1)

j-money
j-money

Reputation: 529

Switching up the order of when I unmarshalled the csv fixed my issue the code now is

CsvDataFormat csv = new CsvDataFormat();

from("file:/pathToFile")
.split(body().tokenize("/n")).streaming()
.unmarshal(csv)
.process(new CsvParserProcess())
.marshal(csv)
.to("file:/pathToOut").log("Finished Transformation").end();

Upvotes: 2

Related Questions