I have a csv file with input content separated with comma ",". I want to convert that into text file with "|" delimiter. I'm using apache camel CsvDataFormat to convert the given csv data format.I can be able to convert the csv content to pipe delimited string. I have two more constants which I have assigned to two variables.I would like to enrich the content of the output data with two additional fields as shown in expected output.
Input
test.csv
Jack Dalton, 115, mad at Averell
Joe Dalton, 105, calming Joe
William Dalton, 105, keeping Joe from killing
Averell Averell Dalton, 80, playing with Rantanplan
Lucky Luke, 120, capturing the Daltons
Test.java
public class Test{
private String name = "Hell0";
private String address = "134 johen rd";
}
ConverterRoute.java
public class ConverterRoute implements RoutesBuilder {
private static final String SOURCE_INPUT_PATH = "file://inbox?fileName=test.csv";
private static final String SOURCE_OUTPUT_PATH = "file://outbox?fileName=file.txt";
public void addRoutesToCamelContext(CamelContext context) throws Exception {
context.addRoutes(new RouteBuilder() {
public void configure() {
try {
CsvDataFormat csvDataFormat = new CsvDataFormat();
csvDataFormat.setDelimiter('|');
csvDataFormat.setHeaderDisabled(true);
from(SOURCE_INPUT_PATH).
unmarshal().csv().
marshal(csvDataFormat).
to(SOURCE_OUTPUT_PATH)
.end();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Output
file.txt
Jack Dalton| 115 | mad at Averell
Joe Dalton| 105 | calming Joe
William Dalton | 105 | keeping Joe from killing
Averell Averell Dalton| 80| playing with Rantanplan
Lucky Luke| 120| capturing the Daltons
Expected Output
file.txt
Jack Dalton| 115 | mad at Averell | Hell0 | 134 johen rd
Joe Dalton| 105 | calming Joe | Hell0 | 134 johen rd
William Dalton | 105 | keeping Joe from killing | Hell0 | 134 johen rd
Averell Averell Dalton| 80| playing with Rantanplan | Hell0 | 134 johen rd
Lucky Luke| 120| capturing the Daltons | Hell0 | 134 johen rd
In the above output file.txt the last two columns are the two constants which I have in my Test.java pojo class. I would like to enrich my pojo fields into the final output. Is there a way I can achieve the result.
In your route after the step
unmarshal().csv()
you got a List<List<String>>
as message body. Every list entry of the outer list represents a CSV line and each inner list contains the values of a line.
What you want to do is to add two values to each inner list.
Since your values are the same for every "line", it is probably the most simple approach to write a plain Java Bean that takes the List<List<String>>
, iterates over the outer list and adds the two values (list entries) to each inner list.
in your Camel route you call this bean with
.bean(YourBean.class, "methodname")
but methodname
is only needed when the Bean has multiple methods.
And then your route continues with
.marshal(csvDataFormat)
.to(SOURCE_OUTPUT_PATH)
This should generate the file you want.
By the way, the .end()
at the end of the route is not needed.