简体   繁体   English

如何在Apache骆驼中使用Splitter EIP将CSV文件一次分割20行?

[英]How to split a csv file 20 lines at a time using Splitter EIP, in apache camel?

I have a csv file having 2000 lines. 我有一个2000行的csv文件 I wish to split it 20 lines at a time and send each to a processor in apache camel. 我希望一次将其拆分为20行 ,并将每发送给使用Apache骆驼的处理器。 How do I do it using Splitter EIP ? 我如何使用Splitter EIP做到这一点? Please help .. 请帮忙 ..

I suggest to spend some time to research the Apache Camel documentation. 我建议花一些时间研究Apache Camel文档。 For example on the Splitter EIP page its all documented how you can do this. 例如,在Splitter EIP页面上,所有内容都记录了如何执行此操作。

See http://camel.apache.org/splitter at the section Splitting files by grouping N lines together 参见http://camel.apache.org/splitter ,位于通过将N行分组在一起来分割文件的部分

package com.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.csv.CsvDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.QuoteMode;

public class FileSplitter {

    public static void main(String args[]) throws Exception {
        CamelContext context = new DefaultCamelContext();
        CsvDataFormat csvParser = new CsvDataFormat(CSVFormat.DEFAULT);
        csvParser.setSkipHeaderRecord(true);
        csvParser.setQuoteMode(QuoteMode.ALL);
        context.addRoutes(new RouteBuilder() {
            public void configure() {
                String fileName = "Hello.csv";
                int lineCount = 20;
                System.out.println("fileName = " + fileName);
                System.out.println("lineCount = " + lineCount);
                from("file:data/inbox?noop=true&fileName=" + fileName).unmarshal(csvParser).split(body()).streaming()
                        .aggregate(constant(true), new ArrayListAggregationStrategy()).completionSize(lineCount)
                        .completionTimeout(1500).marshal(csvParser)
                        .to("file:data/outbox?fileName=${file:name.noext}_${header.CamelSplitIndex}.csv");
            }
        });
        context.start();
        Thread.sleep(10000);
        context.stop();
        System.out.println("End");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM