2016-07-26 111 views
2

我正在嘗試使用駱駝的bindy註釋來解析通過CSV。我在網上看了一些教程,但似乎沒有讓他們工作。我是駱駝這邊的新手,所以我不太明白我得到的錯誤。目前我的CSV非常簡單,因爲我只是想了解這個功能的工作原理。 該CSV目前看起來是這樣的:使用駱駝來解析CSV

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

的錯誤即時得到它這樣的:

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 

    } 

} 
+0

由於某種原因,錯誤日誌沒有打印在我的問題...這裏他們是 –

+0

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

+0

請編輯您的問題與您的錯誤日誌,不要張貼日誌中的評論。 – rmlan

回答

0

對於任何人也可能會遇到這個問題,我發現什麼問題。使用|時作爲你的分隔符,你需要通過它像這樣

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

因爲|是表示OR操作的元字符,我們需要這個操作的正則表達式。

此外,您不能像AlamoHdr文件中的構造函數,因爲變量正在通過綁定填充,而不是通過調用構造函數。