简体   繁体   中英

Parsing csv data format in apache Camel

I followed an example from a book Camel in action. how to marshal and unmarshal csv data format. However, I want to unmarshal a csv file with (comma seperated delimiter) and split body. Then, I will use content based .choice to distribute messages according to required tasks. In fact, The first and simple example didn't work for me. I used camel 2.15.6 (camel-core, camel-context, camel-csv, commons-csv) and java 7.

public void configure()
             {
              CsvDataFormat csv = new CsvDataFormat();
              csv.setDelimiter(",");

              from("file:test?noop=true")
             .unmarshal().csv()
              .split(body())
              .to("file:out");
             } 

Please find below the stack trace.
在此处输入图片说明

Can you try by removing noop=true? Actually, if noop is true, the file is not moved or deleted in any way. This option is good for readonly data, or for ETL type requirements.

Pass csv as a parameter like this:

public void configure()throws Exception
         {
          CsvDataFormat csv = new CsvDataFormat();
          csv.setDelimiter(",");

          from("file:test?noop=true")
            .unmarshal(csv)
            .split(body())
            .to("file:out");
         } 

Or it will help you to set contain based routing:I filter according to header of CSV:

//Route 1 for filter CSV based on header
        from("file:/home/r2/Desktop/csvFile?noop=true")
            .choice().when(body().contains("partyName"))
                .to("direct:partyNameCSV")
            .when(body().contains("\"stuffName\""))
                .to("direct:stuffNameCSV")
            .otherwise().endChoice();   

        //Route 2 partyNameCSV
        from("direct:partyNameCSV")
            .unmarshal(csv)
            .process(new PartyNameCSVProcessor())
            .end();

        //Route 3 stuffNameCSV
        from("direct:stuffNameCSV")
            .unmarshal(csv)
            .process(new StuffCSVProcessor())
            .end();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM