I need to build a csv file based on incoming messages. I do this by appending to the file with:
.toD("file://" + OUTPUT_PATH + "?FileName=${exchangeProperty.OUTPUT_FILENAME}" + "&FileExist=Append")
While this works fine I've run into one problem. I also need to include a header row in the CSV file. Right now I'm marshalling into CSV format with .mashall().csv() but that omits the header.
While I can create a CSV format with header with:
CsvDataFormat csvFormatWithHeader = new CsvDataFormat();
csvFormatWithHeader.setHeader(Arrays.asList(new String[] { "A", "B", "C", "D" }.clone()));
.marshall(csvFormatWithHeader)
That will add the header row for each row I add. So what I want to achieve is to add the header only when a new file is created.
I've been trying two approaches but haven't gotten any to work:
- Check if the file exists in the route and apply the csv format accordingly
- Set the marshall dataformat with a bean or method
As a final option I could add the header when the file is closed but that feels inefficient as I don't know how big that file might become.
How can I achieve either of these approaches with Apache Camel 2.23.2.