Mule将json数组传递给mongodb,但只将第一条记录插入db

时间:2016-05-20 10:52:06

标签: arrays json mongodb mule mule-component

我正在使用Mule 3.6来读取包含100条记录的CSV文件,并将它们转换为JSON数组。然后将JSON作为消息发布到AMQP,然后插入到Mongodb中。

当我查看Mongodb集合时,我只能看到一条记录,但在调试时,它应该是整个阵列直到Mongodb连接器。如何获取Mongo中保存的JSON数组中的所有对象?

另外,我需要将数组中的1个JSON对象保存为Mongodb中的1条记录,我一直在努力使用foreach。

我的流程的XML代码:

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

<mule xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.mulesoft.org/schema/mule/mongo" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:amqp="http://www.mulesoft.org/schema/mule/amqp" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:ftp="http://www.mulesoft.org/schema/mule/ee/ftp" 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.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd
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/ee/ftp http://www.mulesoft.org/schema/mule/ee/ftp/current/mule-ftp-ee.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.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/amqp http://www.mulesoft.org/schema/mule/amqp/current/mule-amqp.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/mongo http://www.mulesoft.org/schema/mule/mongo/current/mule-mongo.xsd">
    <context:property-placeholder location="mule-app.properties" />
    <data-mapper:config name="dept_mapper" transformationGraphPath="dept_mapper.grf" doc:name="dept_mapper"/>
    <amqp:connector name="AMQP_Connector" validateConnections="true" doc:name="AMQP-0-9 Connector"/>
    <mongo:config name="Mongo_DB" username="admin" database="test" doc:name="Mongo DB"/>
    <flow name="read-flow">
        <file:inbound-endpoint path="${file.unprocessed.location}" moveToPattern="#[message.inboundProperties['originalFilename']]" moveToDirectory="${file.processed.location}" responseTimeout="10000" mimeType="text/csv" doc:name="Files">
            <file:filename-regex-filter pattern=".*.csv" caseSensitive="true"/>
        </file:inbound-endpoint>
        <choice doc:name="Choice">
            <when expression="#[wildcard(org.mule.util.StringUtils.upperCase(&quot;dept*&quot;), org.mule.util.StringUtils.upperCase(message.inboundProperties['originalFilename']))]">
                <data-mapper:transform config-ref="dept_mapper" doc:name="dept Mapper"/>
            </when>
            <otherwise>
                <logger level="INFO" doc:name="Logger"/>
            </otherwise>
        </choice>
        <object-to-string-transformer doc:name="Object to String"/>
        <set-variable variableName="exchangeRoutingKey" value="#[wildcard(org.mule.util.StringUtils.upperCase(&quot;dept*&quot;), org.mule.util.StringUtils.upperCase(message.inboundProperties['originalFilename'])) ? 'depts' : (wildcard(org.mule.util.StringUtils.upperCase(&quot;sales*&quot;), org.mule.util.StringUtils.upperCase(message.inboundProperties['originalFilename'])) ? 'sales' : 'employee')]" doc:name="Set Routing Key"/>
        <amqp:outbound-endpoint exchangeName="${rabbitmq.exchange}" routingKey="#[flowVars.exchangeRoutingKey]" exchangeType="topic" responseTimeout="10000" connector-ref="AMQP_Connector" doc:name="Message Queue"/>
    </flow>
    <flow name="read-dept-messages-flow">
        <amqp:inbound-endpoint exchangeName="${rabbitmq.exchange}" queueName="${rabbitmq.queue.depts}" routingKey="${rabbitmq.queue.routing.id.depts}" responseTimeout="10000" exchange-pattern="request-response" connector-ref="AMQP_Connector" doc:name="dept Message Queue"/>
        <flow-ref name="db-processing-subflow" doc:name="db-processing-subflow"/>
    </flow>
    <sub-flow name="db-processing-subflow">
        <object-to-string-transformer doc:name="Object to String"/>
        <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
        <logger level="INFO" doc:name="Logger"/>
        <mongo:json-to-dbobject doc:name="Mongo DB"/>
        <mongo:insert-object config-ref="Mongo_DB" collection="depts" doc:name="Insert debt"/>
    </sub-flow>
</mule>

感谢您的帮助

0 个答案:

没有答案