I have a use case where I am receiving random messages (with a JSON payload) via a queue (RabbitMQ) in Camel and I need to transform these JSON payloads into a CSV format to store in a file. What I have is mostly working, but when I try to add in the "generateHeaderColumns=true" into the @CsvRecord annotation on the POJO I have, I get the column headers repeated for each of the "rows" i end up with. As an example, consider the following route in Camel:
from("rabbitmq://localhost:5672/testexchange?routingKey=create")
.unmarshal().json(JsonLibrary.Jackson, MyModel.class)
.marshal().bindy(BindyType.Csv, MyModel.class)
.to("file://c:/ftproot?fileName=export.$simple{date:now:MM.dd.yyyy}.csv&fileExist=Append")
The following is the MyModel.class POJO:
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
@CsvRecord(separator=",", crlf="UNIX", generateHeaderColumns=true)
public class MyClass
{
@DataField(pos = 1, columnName="System ID")
private String identifier;
@DataField(pos = 2, columnName="Full Name")
private String name;
public void setIdentifier(String identifier)
{
this.identifier = identifier;
}
public String getIdentifier()
{
return identifier;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
However, what I end up with if I send multiple JSON payloads to the queue is the following:
System ID,Full Name
208557,Linus Van Pelt
System ID,Full Name
273053,Charlie Brown
System ID,Full Name
206541,Lucy Van Pelt
System ID,Full Name
279876,Sally Brown
When I look at the code for Bindy, it sort of makes sense what its doing, as the unmarshal step doesn't know that the final result is a file... but how do I get the resulting file to have the CSV record unmarshalled, but only have one "row" of column headers? Is there some other way to do this? In other words, how do I use the queue to receive random messages but have the file end up like this instead?
System ID,Full Name
208557,Linus Van Pelt
273053,Charlie Brown
206541,Lucy Van Pelt
279876,Sally Brown