骆驼CSV显示列名称

时间:2020-07-31 17:16:34

标签: eclipse csv apache-camel camel-sql

我有两个问题。

  1. 第一个问题:我使用camel sql和camel csv在csv中导出数据库。

我想显示列名,但是当我使用骆驼2.16.5版本时,骆驼csv的选项对我不起作用。

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

所有这些测试均无效。...

能否请您帮我解决这个问题,我该如何使其正常工作?

  1. 第二个问题:由于骆驼的csv选项不起作用,我想从camel-csv-2.16.5.jar在CsvDataFormat.class上进行调试 但是我得到了'Source not found The JAR file C:\Utilisateurs\.m2\repository\org\apache\camel\camel-csv-2.16.5.jar has no source attachment'

我通过单击“附加源”手动附加了源,但是仍然看不到正确的源类。 我尝试了Web上已经提到的所有解决方案,但是没有用。

您有什么建议吗?

预先感谢

1 个答案:

答案 0 :(得分:0)

因此CSV数据格式将获取您的整个文件,并将其作为地图返回。然后,您可以将列名用作映射的键​​。

让我演示。我有一个名为customerlist.csv的小型csv文件,看起来像这样:

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

以下内容将读取csv文件,将其转换为地图,然后将其逐行拆分,您可以直接引用列并读取其值。

<?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>

输出为:

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