MuleSoft - 查询数据库,转换并映射到 XML,写入文件

MuleSoft - query database, transform and map to XML, write to file

我是 MuleSoft 的新手。我已经下载了 Anypoint Studio 并正在尝试创建概念验证。我想我很接近,但还不够。基本上这是我想要发生的流程:

  1. 轮询 oracle 数据库以拉回 x 条记录
  2. 将这些记录流式传输到 DataMapper 中,并根据我提供的一些 xsd
  3. 将每条记录放在适当的 XML 标签中
  4. 将 XML 输出写入本地驱动器上的某个文件

我想要的是每秒将一条记录写入 xsd 结构,并为此生成一个新文件。因此,如果数据库中有 10 条记录,则每次轮询数据库查询时都会生成 10 XML 个文件。

下面是我试图根据配置 XML 完成的示例:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:batch="http://www.mulesoft.org/schema/mule/batch" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/batch http://www.mulesoft.org/schema/mule/batch/current/mule-batch.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
    <db:oracle-config name="#######" host="#######" port="####" instance="#######" user="#####" password="######" doc:name="Oracle Configuration"/>
    <file:connector name="XML_File" outputPattern="#[payload.planner_id]-#[server.dateTime]-.xml" autoDelete="true" streaming="true" validateConnections="true" doc:name="File" outputAppend="true" recursive="true" writeToDirectory="C:\Users\gm840d\Documents\Projects\IMM\AnypointStudio\FileOutput"/>
    <data-mapper:config name="List_Map__To_XML" transformationGraphPath="list_map__to_xml.grf" doc:name="List_Map__To_XML"/>
    <data-mapper:config name="List_Map__To_XML_1" transformationGraphPath="list_map__to_xml_1.grf" doc:name="List_Map__To_XML_1"/>
    <batch:job name="project_testxmlBatch" >
        <batch:input>
            <poll doc:name="Poll">
                <fixed-frequency-scheduler frequency="10" timeUnit="SECONDS"/>
                <processor-chain doc:name="Processor Chain">
                    <db:select config-ref="Ora_IMCT" doc:name="Database">
                        <db:parameterized-query><![CDATA[select planner, name, work_center
from schema.mytable]]></db:parameterized-query>
                    </db:select>
                </processor-chain>
            </poll>
        </batch:input>
        <batch:process-records>
            <batch:step name="Batch_Step" accept-policy="ALL">
                <batch:commit  doc:name="Batch Commit" size="306">
                    <data-mapper:transform config-ref="List_Map__To_XML_1" doc:name="List&lt;Map&gt; To XML"/>
                    <file:outbound-endpoint path="C:\Users\gm840d\Documents\Projects\IMM\AnypointStudio\FileOutput"  responseTimeout="10000" encoding="UTF-8" mimeType="text/xml" doc:name="File" connector-ref="XML_File"/>
                </batch:commit>
            </batch:step>
        </batch:process-records>
        <batch:on-complete>
            <logger message="Total Records exported: #[message.payload.getLoadedRecords()], Processing time: #[message.payload.getElapsedTimeInMillis()]app" level="WARN" doc:name="Logger"/>
        </batch:on-complete>
    </batch:job>
</mule>

似乎当我 运行 mule 应用程序时,它一直工作到提取所有必要记录的地步,但在那之后就停止了。没有生成文件或任何东西。这是每隔几秒 "poll" 运行 时控制台输出的一个片段。

*******************************************************************************************************
*            - - + APPLICATION + - -            *       - - + DOMAIN + - -       * - - + STATUS + - - *
*******************************************************************************************************
* project_testxml                               * default                        * DEPLOYED           *
*******************************************************************************************************

INFO  2015-02-27 10:41:23,124 [pool-14-thread-1] com.mulesoft.module.batch.engine.DefaultBatchEngine: Created instance 3a62ec2d-beb0-11e4-adb3-ecf4bb3a71c5 for batch job project_testxmlBatch
INFO  2015-02-27 10:41:23,124 [pool-14-thread-1] com.mulesoft.module.batch.engine.DefaultBatchEngine: Starting input phase
INFO  2015-02-27 10:41:23,124 [pool-14-thread-1] com.mulesoft.module.batch.engine.DefaultBatchEngine: Input phase completed
INFO  2015-02-27 10:41:23,144 [pool-14-thread-1] com.mulesoft.module.batch.engine.queue.BatchQueueLoader: Starting loading phase for instance '3a62ec2d-beb0-11e4-adb3-ecf4bb3a71c5' of job 'project_testxmlBatch'
INFO  2015-02-27 10:41:23,236 [pool-14-thread-1] com.mulesoft.module.batch.engine.queue.BatchQueueLoader: Finished loading phase for instance 3a62ec2d-beb0-11e4-adb3-ecf4bb3a71c5 of job project_testxmlBatch. 306 records were loaded
INFO  2015-02-27 10:41:23,256 [pool-14-thread-1] com.mulesoft.module.batch.engine.DefaultBatchEngine: Started execution of instance '3a62ec2d-beb0-11e4-adb3-ecf4bb3a71c5' of job 'project_testxmlBatch'
INFO  2015-02-27 10:41:32,256 [pool-14-thread-1] com.mulesoft.module.batch.engine.DefaultBatchEngine: Created instance 3fd5e4a4-beb0-11e4-adb3-ecf4bb3a71c5 for batch job project_testxmlBatch
INFO  2015-02-27 10:41:32,256 [pool-14-thread-1] com.mulesoft.module.batch.engine.DefaultBatchEngine: Starting input phase
INFO  2015-02-27 10:41:32,256 [pool-14-thread-1] com.mulesoft.module.batch.engine.DefaultBatchEngine: Input phase completed
INFO  2015-02-27 10:41:32,256 [pool-14-thread-1] com.mulesoft.module.batch.engine.queue.BatchQueueLoader: Starting loading phase for instance '3fd5e4a4-beb0-11e4-adb3-ecf4bb3a71c5' of job 'project_testxmlBatch'
INFO  2015-02-27 10:41:32,327 [pool-14-thread-1] com.mulesoft.module.batch.engine.queue.BatchQueueLoader: Finished loading phase for instance 3fd5e4a4-beb0-11e4-adb3-ecf4bb3a71c5 of job project_testxmlBatch. 306 records were loaded
INFO  2015-02-27 10:41:32,327 [pool-14-thread-1] com.mulesoft.module.batch.engine.DefaultBatchEngine: Started execution of instance '3fd5e4a4-beb0-11e4-adb3-ecf4bb3a71c5' of job 'project_testxmlBatch'

如有任何帮助,我们将不胜感激。一直在 Google 中寻找这样的例子,但一无所获。

在批处理记录的batch-step中,在datmapper之前添加collection splitter,并为每个记录格式实现您为xsd定义指定的逻辑,并按照您指定的方式将其写入文件。

你的 datamapper 映射有问题,它无法转换从数据库中提取的数据,因此 returns 空负载,这就是你的 sftp 连接器无法创建文件的原因。

此外,如果您想为每条记录生成每个文件,那么您应该使用集合拆分器并在轮询连接器中使用水印功能。