Header 的 Apache Camel CSV

Apache Camel CSV with Header

我编写了一个简单的测试应用程序,它从数据库中读取记录并将结果放入 csv 文件中。到目前为止它工作正常但是列名即 headers 没有放在 csv 文件中。根据文档,它应该放在那里。我也试过 without/with streaming 和 split 但情况是一样的。

在 camel unit-tests 的第 182 行中,header 明确地放在那里:https://github.com/apache/camel/blob/master/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatTest.java

如何在不需要遍历 header 的情况下解决这个非常简单的问题?我也尝试了不同的设置,但都是一样的。例如定界符被认为是我设置的,但 headers 不是。也提前感谢您的回复。

我是这样使用 Camel 2.16.1 的:

final CsvDataFormat csvDataFormat = new CsvDataFormat();
csvDataFormat.setHeaderDisabled(false);
[...]
from("direct:TEST").routeId("TEST")
        .setBody(constant("SELECT * FROM MYTABLE"))
        .to("jdbc:myDataSource?readSize=100") // max 100 records
//      .split(simple("${body}"))             // split the list
//      .streaming()                          // not to keep all messages in memory
        .marshal(csvDataFormat)
        .to("file:extract?fileName=TEST.csv");
[...]

编辑 1

我也尝试从 exchange.in 添加 header。它们在 HashSet 中可用,名称为 "CamelJdbcColumnNames"。我像这样将它添加到 csvDataFormat 中:

    final CsvDataFormat csvDataFormat = new CsvDataFormat();
    csvDataFormat.setHeaderDisabled(false);
    [...]
    from("direct:TEST").routeId("TEST")
            .setBody(constant("SELECT * FROM MYTABLE"))
            .to("jdbc:myDataSource?readSize=100") // max 100 records
            .process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                    headerNames =                       (HashSet)exchange.getIn().getHeader("CamelJdbcColumnNames");
                    System.out.println("#### Process headernames = " + new ArrayList<String>(headerNames).toString());
                     csvDataFormat.setHeader(new ArrayList<String>(headerNames));
                         }
                })
                .marshal(csvDataFormat)//.tracing()
                .to("file:extract?fileName=TEST.csv");

println() 会打印列名,但生成的 cvs 文件不会。

EDIT2 我按照评论 1 中的提议将 header 名称添加到 body,如下所示:

.process(new Processor() {
    public void process(Exchange exchange) throws Exception {

        Set<String> headerNames = (HashSet)exchange.getIn().getHeader("CamelJdbcColumnNames");
        Map<String, String> nameMap = new LinkedHashMap<String, String>();
        for (String name: headerNames){
            nameMap.put(name, name);
        }
        List<Map> listWithHeaders = new ArrayList<Map>();
        listWithHeaders.add(nameMap);

        List<Map> records = exchange.getIn().getBody(List.class);
        listWithHeaders.addAll(records);
        exchange.getIn().setBody(listWithHeaders, List.class);


        System.out.println("#### Process headernames = " + new ArrayList<String>(headerNames).toString());
        csvDataFormat.setHeader(new ArrayList<String>(headerNames));
     }
})

该提案解决了问题,谢谢你,但这意味着 CsvDataFormat 并不真正可用。 JDBC 查询之后的交换 body 包含来自 HashMaps 的 ArrayList,其中包含 table 的一条记录。 HashMap的key是列名,value是值。因此,在 CsvDataFormat 中设置 header 输出的配置值应该足以生成 headers。您知道更简单的解决方案还是我错过了配置中的某些内容?

您使用 JDBC 从数据库中获取数据,因此您需要先将 headers 添加到消息 body 中,因此它是第一行。 jdbc 的结果集只是数据,不包括 headers.

我已经通过覆盖 BindyCsvDataFormat 和 BindyCsvFactory 完成了

public class BindySplittedCsvDataFormat extends BindyCsvDataFormat {

    private boolean marshallingfirslLot = false;

    public BindySplittedCsvDataFormat() {
        super();
    }

    public BindySplittedCsvDataFormat(Class<?> type) {
        super(type);
    }

    @Override
    public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws Exception {
        marshallingfirslLot = new Integer(0).equals(exchange.getProperty("CamelSplitIndex"));
        super.marshal(exchange, body, outputStream);
    }

    @Override
    protected BindyAbstractFactory createModelFactory(FormatFactory formatFactory) throws Exception {
        BindySplittedCsvFactory bindyCsvFactory = new BindySplittedCsvFactory(getClassType(), this);
        bindyCsvFactory.setFormatFactory(formatFactory);
        return bindyCsvFactory;
    }

    protected boolean isMarshallingFirslLot() {
        return marshallingfirslLot;
    }
}


public class BindySplittedCsvFactory extends BindyCsvFactory {

    private BindySplittedCsvDataFormat bindySplittedCsvDataFormat;

    public BindySplittedCsvFactory(Class<?> type, BindySplittedCsvDataFormat bindySplittedCsvDataFormat) throws Exception {
        super(type);
        this.bindySplittedCsvDataFormat = bindySplittedCsvDataFormat;
    }

    @Override
    public boolean getGenerateHeaderColumnNames() {
        return super.getGenerateHeaderColumnNames() && bindySplittedCsvDataFormat.isMarshallingFirslLot();
    }

}

我使用 spring xml 的解决方案(但我想有一个选项用于提取顶部的 header:

使用 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/