在Apache Camel中输出CSV列标题

时间:2015-08-06 21:14:32

标签: java csv apache-camel marshalling

是否可以在此页面的“将地图编组为CSV”示例中输出标题行? http://camel.apache.org/csv.html

Map<String, Object> body = new LinkedHashMap<>();
body.put("foo", "abc");
body.put("bar", 123);

from("direct:start")
.marshal().csv()
.to("mock:result");

输出:

abc,123

期望的输出:

foo,bar
abc,123

3 个答案:

答案 0 :(得分:1)

您需要使用Camel 2.15以支持配置标头。请参阅文档:http://camel.apache.org/csv.html

camel-csv的单元测试有一些例子 https://github.com/apache/camel/tree/master/components/camel-csv

答案 1 :(得分:0)

我定义了List<Map<String,String>>的聚合器,该聚合器在第一次调用时将Map.get(0).keySet()添加为聚合List的元素0(例如oldExchange==null)。聚合完成后,我在结果上调用了marshal().csv()

答案 2 :(得分:0)

使用spring xml

Using spring xml

      <multicast stopOnException="true">
    <pipeline>
      <log message="saving table ${headers.tablename} header to ${headers.CamelFileName}..."/>
      <setBody>     
<groovy>request.headers.get('CamelJdbcColumnNames').join(";") + "\n"</groovy>
      </setBody>
      <to uri="file:output"/>
    </pipeline>

    <pipeline>
      <log message="saving table ${headers.tablename} rows to ${headers.CamelFileName}..."/>
      <marshal>
        <csv delimiter=";" headerDisabled="false" useMaps="true"/>
      </marshal>
      <to uri="file:output?fileExist=Append"/>
    </pipeline>
  </multicast>

http://www.redaelli.org/matteo-blog/2019/05/24/exporting-database-tables-to-csv-files-with-apache-camel/

再见哑光