使用Smooks在WSO2 ESB 5.0中逐行读取CSV文件

时间:2017-10-29 14:24:02

标签: csv wso2 wso2esb transformation smooks

我有一个我通过VFS阅读的CSV文件。在阅读文件后,我使用Smooks将CSV数据转换为XML,然后将其发送到另一个代理。 默认情况下,smooks将整个消息转换为一个xml有效负载。

问题:

默认方法对于小文件是可以的,但我有一个非常大的文件要处理,我希望我可以逐行读取文件,然后将消息发送到下一个组件。

代理配置:

<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="ParseTestCSV1"
       startOnLoad="true"
       statistics="disable"
       trace="disable"
       transports="vfs">
   <target>
      <inSequence>
         <log level="full" separator="*********Parsing Prroxy Started*****"/>
         <smooks config-key="gov:/repository/csv/smooks-config.xml">
            <input type="text"/>
            <output type="xml"/>
         </smooks>
         <log level="full" separator="********After Smooks*******"/>
         <property name="OUT_ONLY" value="true"/>
         <property action="remove" name="ClientApiNonBlocking" scope="axis2"/>
         <call/>
         <log level="full" separator="*******Message Sent*********"/>
      </inSequence>
      <outSequence/>
      <faultSequence/>
   </target>
   <parameter name="transport.PollInterval">60</parameter>
   <parameter name="transport.vfs.FileURI">vfs:file://C:\WSO2EnterpriseIntegrator\file\In</parameter>
   <parameter name="transport.vfs.ContentType">text/plain</parameter>
   <parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
   <parameter name="transport.vfs.MoveAfterFailure">vfs:file://C:\WSO2EnterpriseIntegrator\file\Fail</parameter>
   <parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>
   <parameter name="transport.vfs.FileNamePattern">convert.*.csv</parameter>
   <parameter name="transport.vfs.MoveAfterProcess">vfs:file://C:\WSO2EnterpriseIntegrator\file\Out</parameter>
   <description/>
</proxy>

Smooks配置文件:

<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.2.xsd">

        <resource-config selector="org.xml.sax.driver">

            <resource>org.milyn.csv.CSVReader</resource>

            <param name="fields">$ignore$,firstname,lastname,age,street,address,statecode,postalcode,amount,code,date</param>

            <param name="rootElementName">record</param>

            <param name="recordElementName">csvRecord</param>

        </resource-config>

    </smooks-resource-list>

如何将Smook的配置文件转换为读取一行然后向前发送。任何见解将不胜感激。

1 个答案:

答案 0 :(得分:1)

我有类似的情况,我必须将XML中的大量XML拆分为jms消息。您可以使用freemaker模板实现此目的。

基本上,您定义了自由标记模板并将其绑定到路由器。

示例:

    <?xml version="1.0" encoding="UTF-8"?>
<!--
 ~ Copyright (c) 2005-2010, WSO2 Inc. (http://wso2.com) All Rights Reserved.
 ~
 ~ WSO2 Inc. licenses this file to you under the Apache License,
 ~ Version 2.0 (the "License"); you may not use this file except
 ~ in compliance with the License.
 ~ You may obtain a copy of the License at
 ~
 ~    http://www.apache.org/licenses/LICENSE-2.0
 ~
 ~ Unless required by applicable law or agreed to in writing,
 ~ software distributed under the License is distributed on an
 ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 ~ KIND, either express or implied.  See the License for the
 ~ specific language governing permissions and limitations
 ~ under the License.
 -->
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                      xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.3.xsd"
                      xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd"
                      xmlns:file="http://www.milyn.org/xsd/smooks/file-routing-1.1.xsd"
                      xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"
                      xmlns:jms="http://www.milyn.org/xsd/smooks/jms-routing-1.2.xsd">


                <core:namespaces>
        <core:namespace prefix="soapenv" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
        </core:namespaces>

    <core:filterSettings type="SAX" />
    <jb:bean beanId="recordsAsXml" class="java.util.Hashtable" createOnElement="records">
     <jb:value data="records/Id" decoder="String" property="Id"></jb:value>
     <jb:value data="records/Account_ID__c" decoder="String" property="accountID"></jb:value> 
     <!-- ...-->
 </jb:bean>

    <ftl:freemarker applyOnElement="records">
        <!--<ftl:template>/repository/resources/smooks/record_as_xml.ftl</ftl:template>-->
        <ftl:template><!--<order>
        <id>${recordsAsXml.Id}</id>
        <accountId>${recordsAsXml.accountID}</accountId>
        <!-- ...-->
   </order>-->
        </ftl:template>
        <ftl:use>
            <ftl:bindTo id="recordAsXmlOutput"/>
        </ftl:use>
    </ftl:freemarker>


    <jms:router routeOnElement="records" beanId="recordAsXmlOutput" destination="MyJMSQueue">
        <jms:message>
        </jms:message>
        <jms:jndi properties="/repository/resources/smooks/activemq.sr.jndi.properties" />
        <jms:highWaterMark mark="-1"/> 
    </jms:router>
</smooks-resource-list>

以下文章也可能有所帮助。

Message processing with smooks