0
votes

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.
enter image description here

2
the stacktrace doesn't indicate anything. When you say it didn't work for you. What is happening.pvpkiran
The program terminated without copying a file .to("file:out");Chayma Sakouhi
where is your file test? check if the path is correct.pvpkiran
The path is correct, because Camel locks the file (for example test\standard.csv.camelLock ), then, the program shut down. I no know why unmarshalling does not work.Chayma Sakouhi
try without split. I think split function requires a token to split which you are not specifyingpvpkiran

2 Answers

0
votes

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.

0
votes

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();