使用Spring DSL解组,操作然后在Camel中编组CVS数据

时间:2015-03-20 14:19:58

标签: spring csv apache-camel marshalling unmarshalling

我是Camel / Spring DSL的新手。我正在尝试使用Camel 2.14.1解组CSV数据文件,操纵数据列(移位列并将算术运算应用于这些列中的值),然后将数据封送回新的CSV数据文件。

我的日志消息表明我可以将我的CSV数据解组为Java List<Map<String,Object>>集合,然后将该集合封送回CSV数据(但未更改,但输入CSV数据中的列标题丢失除外)。

  1. 如何在Spring DSL中访问unmarshalled Java集合?

  2. 我可以只在Spring DSL中实现所有操作吗?或者我必须实现并使用Java Bean吗?

  3. 为什么在我的解组/编组过程中丢失了我的CSV数据中的列标题?

  4. 我发现一些帖子建议使用来自Apache Commons CSV的CSVStrategyCSVConfig,但并不是特别清楚。这是正确的方向吗?

  5. 我应该补充一点,我已经使用Java DSL实现了相同的练习,其中Camel处理器可以在Camel Exchange对象上运行。

    我的输入CSV测试数据如下所示:

    ColumnA,ColumnB
    10,11
    20,22
    30,33
    

    我的输出CSV测试数据如下所示:

    10,11
    20,22
    30,33
    

    My Spring DSL文件位于下方。

       <?xml version="1.0" encoding="UTF-8"?>
    <beans>
        <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
            <property name="connectionFactory">
                <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                    <property name="brokerURL" value="tcp://localhost:61616" />
                </bean>
            </property>
        </bean>
        <!-- requires camel-spring.jar -->
        <camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
            <route id="CsvFileToQueueRoute">
                <!-- it seems that CSV files need column headings in first line -->
                <from uri="file:data/input?include=.*.csv&amp;noop=true" />
                <log
                    message="Reading file from data/input : ${header.CamelFileAbsolutePath}" />
                <!-- convert CSV data into Java List-Map data (see useMaps=true) -->
                <unmarshal>
                    <csv delimiter="," useMaps="true" />
                </unmarshal>
                <log message="Reading file from data/input : ${body}" />
                <to uri="activemq:csv.data" />
            </route>
            <route id="QueueToFileRoute">
                <from uri="activemq:csv.data" />
                <marshal>
                    <csv autogenColumns="true" delimiter="," useMaps="true" />
                </marshal>
                <log message="Writing message from activemq:csv.data : ${headers}" />
                <to
                    uri="file:data/output/csv.data?fileName=${date:now:yyyy-MM-dd_HH-mm-ss.SSS}.csv" />
            </route>
        </camelContext>
    </beans>
    

    我使用Spring DSL文件的简短Java程序如下所示。

    import org.apache.camel.CamelContext;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class CsvUnmarshalSpringDsl
    {
        public static final Logger LOGGER = LoggerFactory.getLogger(CsvUnmarshalSpringDsl.class);
    
        public static void main(String[] args) throws Exception
        {
            LOGGER.info("CsvUnmarshalSpringDsl");
    
            ApplicationContext myAppContext = new ClassPathXmlApplicationContext(
                "file:src/main/resources/CsvUnmarshalSpringDsl.xml");
    
            CamelContext myCamelContext = (CamelContext) myAppContext.getBean("camelContext");
    
            myCamelContext.start();
            Thread.sleep(10000);
            myCamelContext.stop();
            ((ConfigurableApplicationContext) myAppContext).close();
        }
    }
    

0 个答案:

没有答案