1

I am writing a CSV splitter in camel. My requirements are these:

  1. The first line would be header.
  2. The header is not defined. But can contain any no of attributes
  3. I need to split the file and each split then contains a map of key value attributes.

Sample csv1:

header1, header2, header3
value11, value12 , value13
value21, value22 , value23

Expected splits:

{"header1":"value11", "header2": "value12", "header3": "value13"}, 
{"header1":"value21", "header2": "value22", "header3": "value23"}

Sample csv2:

header1, header2
value11, value12
value21, value22

Expected splits:

{"header1":"value11", "header2": "value12"}, 
{"header1":"value21", "header2": "value22"}

How can I achieve this in camel using either Bindy or BeanIO?

  • Can you elaborate on the use case for this? Seems like a very strange design for an integration – Daniel May 16 '14 at 04:56
  • These are csv files that are being passed by vendors to us. These contain information about the products that the vendor will sell to us. The headers contain information about the attributes that the vendor provides: like stock_size, sla, price, discount, offers. The requirement is that the headers are not fixed but have to be interpreted per vendor. We have a rule engine that would read these headers and then translate them into a set of attributes that our system can understand. – user3487839 May 16 '14 at 05:27
  • Use the old school Java way: write a bean Java class that parses the input and generates the output? – Peter Keller May 16 '14 at 15:47

1 Answers1

0

We can use the following code.

byte[] bytes = exchange.getIn().getBody(byte[].class);
    InputStream inputStream = new ByteArrayInputStream(bytes);
    Reader in = new InputStreamReader(inputStream, "UTF-8");
    Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader().parse(in);
    for (CSVRecord csvRecord : records) {

        String header1= csvRecord.get("header1");
        String header2 = csvRecord.get("header2");

    // We can form json here

        System.out.println("header1 :"+header1);
        System.out.println("header2 :"+header2);

    }
Sridhar
  • 89
  • 7