使用Camel解析CSV

时间:2016-07-26 13:20:54

标签: java csv apache-camel

我正在尝试使用Camel的bindy注释来解析CSV。我已经在线跟踪了一些教程,但似乎无法让它们起作用。我是骆驼这边的新手,所以我不太明白我得到的错误。目前我的CSV非常简单,因为我只想了解这个功能是如何工作的。 CSV目前看起来像这样:

HDR | Suborg | COUNTRYCODE | BrokerFile |批量|时间|日期|

错误我得到了这个:

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

这是我的代码:

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

    }

}

1 个答案:

答案 0 :(得分:0)

对于那些也可能遇到这个问题的人,我发现了什么问题。使用|时作为你的分隔符,你需要像这样传递它

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

因为|是一个表示OR运算的元字符,我们需要一个正则表达式来执行此操作。

此外,您不能在AlamoHdr等文件中使用构造函数,因为变量是通过绑定而不是通过调用构造函数来填充的。