camel csv display column name

427 views Asked by At

I have two questions.

  1. The first question: i use camel sql and camel csv to export database in csv.

I would like to display column names however the options of camel csv doesn't work for me while i am on camel 2.16.5 version.

ex) <csv delimiter=";" skipHeaderRecord="false" />  or <csv delimiter=";" headerDisabled="false" /> 
    or
    <csv >
       <header>orderId</header>
       <header>amount</header>
    </csv>

All these test didn't work....

Could you please help me to solve this problem how can i make it work ?

  1. The seconde question : As camel csv options don't work i wanted to debug on CsvDataFormat.class from camel-csv-2.16.5.jar But i got 'Source not found The JAR file C:\Utilisateurs\.m2\repository\org\apache\camel\camel-csv-2.16.5.jar has no source attachment'.

I attached the source manually by clicking 'Attach source' but i still can not see the proper source class. I tried every solution already mentioned in web but not working.

Do you have any advice ?

Thanks in advance

1

There are 1 answers

0
Namphibian On

So CSV dataformat will take your entire file and return it as a Map. You can then use the column names as keys into the map.

Let me demonstrate. I have a small csv file called customerlist.csv which looks like this:

name,surname
Michael,SMITH
James,JOHNSON
John,WILLIAMS
Robert,BROWN
David,JONES
William,MILLER
Mary,DAVIS
Christopher,GARCIA
Joseph,RODRIGUEZ

The following will read the csv file turn it into a map and then split it line by line and you can reference the columns directly and read their values.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring https://camel.apache.org/schema/spring/camel-spring.xsd">



    <camelContext id="work-order-context" streamCache="true"
                  trace="false" xmlns="http://camel.apache.org/schema/spring"
                  messageHistory="true">

        <dataFormats>

            <csv id="csv" useMaps="true" ignoreEmptyLines="true"/>
        </dataFormats>

        <route id="readCsvRoute">
            <from uri="file:src/main/resources/file?noop=true&amp;delay=5000&amp;idempotent=false&amp;sendEmptyMessageWhenIdle=true"/>
            <log message="this is the raw file: ${body}"/>
            <unmarshal>
                <custom ref="csv"/>
            </unmarshal>
            <log message="this is the csv format: ${body}"/>
            <split parallelProcessing="false">
                <simple>${body}</simple>
                <log message="line ${headers.CamelSplitIndex} contains: ${body}"/>
                <log message=" To get the name I can use the map with body.get(name) which equals: ${body.get(name)}" />
                <log message=" To get the surname I can use the map with body.get(surname) which equals: ${body.get(surname)}" />
            </split>
        </route>


    </camelContext>
</beans>

The output would be:

2020-08-13 14:07:46.025  INFO 44843 --- [/resources/file] readCsvRoute                             : line 0 contains: {name=Michael, surname=SMITH}
2020-08-13 14:07:46.051  INFO 44843 --- [/resources/file] readCsvRoute                             :  To get the name I can use the map with body.get(name) which equals: Michael
2020-08-13 14:07:46.052  INFO 44843 --- [/resources/file] readCsvRoute                             :  To get the surname I can use the map with body.get(surname) which equals: SMITH