Connor Blair Connor Blair - 4 months ago 29
Java Question

Using Camel to Parse CSV

I am attempting to use Camel's bindy annotation to parse through a CSV. I have followed some tutorials online but cannnot seem to get them to work. I am new to this side of camel, so I dont quite understand the errors I am getting. For right now my CSV is very simple as I am just trying to understand how this functionallity works.
The CSV currently looks likes this:

HDR|Suborg|CountryCode|BrokerFile|Batch|Time|Date|

The Error Im getting it this:

org.apache.camel.RuntimeCamelException: java.lang.InstantiationException: com.ups.ttg.bsis.fromdos.AlamoHdr

Here is my code:

public class AlamoPipeRouteBuilder extends RouteBuilder {

final DataFormat bindy = new BindyCsvDataFormat(AlamoHdr.class);

/*
* Endpoints
*/
@EnforceInitialization
private Logging logging;

@EnforceInitialization
private RatingProfileAlamoSplitHandler ratingProfileAlamoSplitHandler;

@EnforceInitialization
private String start = "";

@EnforceInitialization
private String end = "";

@Override
public void configure() throws Exception {
System.out.println("Started Configure Method");

/*
* Basic Route
*/
from(start)
.setExchangePattern(ExchangePattern.InOnly)
.routeId("processRatingProfile.alamo")
//.beanRef("RatingProfileExchangeUtilies", "checkForNoRecords(*)")
.beanRef("logging", "debug(*, 'Starting aggregation strategy loop...')")
.split(body().tokenize("\n"), ratingProfileAlamoSplitHandler).streaming()
.unmarshal(bindy)
.setHeader("INDEX", simple("${header.CamelSplitIndex}") )
.setHeader("COMPLETE", simple("${header.CamelSplitComplete}") )
.end()
.beanRef("logging", "debug(*, 'Aggregation strategy loop complete...')")
.removeHeader("lastRatingProfile")
.to(end);
}

public void setStart(String start) {
this.start = start;
}

public void setEnd(String end) {
this.end = end;
}

public void setRatingProfileAlamoSplitHandler(RatingProfileAlamoSplitHandler ratingProfileAlamoSplitHandler) {
this.ratingProfileAlamoSplitHandler = ratingProfileAlamoSplitHandler;
}

public void setLogging(Logging logging) {
this.logging = logging;
}
}



public class RatingProfileAlamoSplitHandler implements AggregationStrategy {

@EnforceInitialization
private static Logging logging;

public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
Integer currIndex = -1;
boolean lastLine = false;

if(newExchange != null) {
currIndex = (Integer) newExchange.getIn().getHeader("INDEX");
System.out.println("THIS IS THE INDEX: " + currIndex);

/*lastLine = (Boolean) newExchange.getIn().getHeader("COMPLETE");
System.out.println("THIS IS THE COMPLETE: " + lastLine);*/

System.out.println("This IS THE BODY: " + newExchange.getIn().getBody());

if(currIndex == 0) {
AlamoHdr alamoHdr = (AlamoHdr) newExchange.getIn().getBody();
}
}

return newExchange;
}

public static void setLogging(Logging logging) {
RatingProfileAlamoSplitHandler.logging = logging;
}

}

public class AlamoHdr implements InitializingBean, DisposableBean {

@DataField(pos = 2, trim = true)
private String suborg;

@DataField(pos = 3, trim = true)
private String countryCode;

@DataField(pos = 4, trim = true)
private String brokerFile;

@DataField(pos = 5, trim = true)
private String batch;

@DataField(pos = 6, trim = true)
private String time;

@DataField(pos = 7, trim = true)
private String date;

public AlamoHdr(String suborg, String countryCode, String brokerFile, String batch, String time, String date) {
super();
this.suborg = suborg;
this.countryCode = countryCode;
this.brokerFile = brokerFile;
this.batch = batch;
this.time = time;
this.date = date;
}

public String getSuborg() {
return suborg;
}

public void setSuborg(String suborg) {
this.suborg = suborg;
}

public String getCountryCode() {
return countryCode;
}

public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}

public String getBrokerFile() {
return brokerFile;
}

public void setBrokerFile(String brokerFile) {
this.brokerFile = brokerFile;
}

public String getBatch() {
return batch;
}

public void setBatch(String batch) {
this.batch = batch;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

@Override
public String toString() {
return "AlamoHdr [suborg=" + suborg + ", countryCode=" + countryCode + ", brokerFile=" + brokerFile + ", batch="
+ batch + ", time=" + time + ", date=" + date + "]";
}

public void destroy() throws Exception {
// TODO Auto-generated method stub

}

public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub

}

}

Answer

For anyone who may also come across this problem, I found what was going wrong. When using a | as your separator you need to pass it in like so

@CsvRecord(separator = "\\|", skipFirstLine = false)

because the | is a meta character that represents the OR operation and we need a regular expression for this operation.

Also you cannot have a constructor in files like AlamoHdr because the variables are being populated through the binding not through calling the constructor.