Mule 4 - 无法将并行 for_each 循环的结果合并到单个 json 中

发布于 2025-01-10 15:01:40 字数 846 浏览 0 评论 0原文

在 Mule 4 的流程中,我迭代多个文件以从中获取数据并将它们转换为 json。我想将每次迭代的结果合并到一个 json 输出中。 我使用变量来存储结果,但它仍然是空的。 以下是我想要组合的输出

结果 1:

{
"plant": "CD909837289",
"serial": "SRF",
"product": "CMNPSD"
},
{
 "plant": "CD909837290",
 "serial": "SFG",
 "product": "CMNHSA"
}

结果 2:

{
"plant": "CD909837296",
"serial": "SFG",
"product": "ERTYUI"
},
{
 "plant": "CD909837297",
 "serial": "SVH",
 "product": "SDFGHJ"
}

我想将它们组合在一起。 我设置了一个这样的变量:

<set-variable doc:name="Set Variable" doc:id="d2bd8c47-da05-4847-b5d5-f83331a1d011" variableName="STORED_DATA" value="#[[]]"/>

一旦有效负载被转换,我添加另一个转换消息组件,将转换后的有效负载添加到变量中

%dw 2.0
output application/json
---
vars.STORED_DATA ++ payload

当我这样做时,变量保持为空。在记录器组件I中,变量的值仍然是[]。 如何确保结果存储在变量中?

我感谢所有的帮助。

In my flow in Mule 4, I iterate over multiple files to get data from them and transform them to json. I want to merge the results of each iteration into a single json output.
I am using a variable to store the results, but it remains empty.
Here's are the outputs I want to combine

result 1:

{
"plant": "CD909837289",
"serial": "SRF",
"product": "CMNPSD"
},
{
 "plant": "CD909837290",
 "serial": "SFG",
 "product": "CMNHSA"
}

result 2:

{
"plant": "CD909837296",
"serial": "SFG",
"product": "ERTYUI"
},
{
 "plant": "CD909837297",
 "serial": "SVH",
 "product": "SDFGHJ"
}

I want to combine them together.
I set a variable like this:

<set-variable doc:name="Set Variable" doc:id="d2bd8c47-da05-4847-b5d5-f83331a1d011" variableName="STORED_DATA" value="#[[]]"/>

Once the payload is transformed I add another transform message component where I add the transformed payload to the variable

%dw 2.0
output application/json
---
vars.STORED_DATA ++ payload

When I do this the variable remains empty. In the logger component I the value of the variable is still [].
How do I make sure the results are stored in the variable?

I appreciate all help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

壹場煙雨 2025-01-17 15:01:40

正如文档提到的,并行 foreach 范围内的变量更改在其外部无法访问。这是有道理的,因为我们正在处理并行性,并且会导致各种并发问题。

并行 foreach 返回每次执行返回的所有消息的列表。每条消息都包含其有效负载。您已经在输出中获得了所有需要的数据。要将其转换为 JSON,您只需要在 foreach 之后进行与此类似的转换:

output application/JSON
---
payload map $.payload

我现在无法测试它,因此可能需要一些调整。

As the documentation mentions the changes to variables inside the parallel foreach scope are not accessible outside it. This makes sense because we are dealing with parallelism and would cause all kinds of concurrency issues.

The parallel foreach returns a list of all the messages returned by each execution. Each message contains it's payload. You already have all the needed data in the output. To transform it to JSON you just need a transformation similar to this after the foreach:

output application/JSON
---
payload map $.payload

I can't test it right now so it may need some tweaking.

瑕疵 2025-01-17 15:01:40

请检查下面的代码,它会对您有所帮助。我正在设置 null 变量并连接 httpRequest 响应。

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

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <flow name="Testing_foreach_stackoverflowFlow" doc:id="95fe6a04-4aca-4106-a0b3-b53afb7be95a" >
        <scheduler doc:name="Scheduler" doc:id="e6a81b1c-43f9-486a-a451-b31bcadbe23f" >
            <scheduling-strategy >
                <fixed-frequency frequency="5" timeUnit="MINUTES"/>
            </scheduling-strategy>
        </scheduler>
        <set-variable value="#[%dw 2.0



output application/json



---



[]]" doc:name="Set Variable" doc:id="7b1bbba6-d63d-4f89-905b-8b9c7d6a7555" variableName="result"/>
        <set-payload value='#[["test1","test2"]]' doc:name="Set Payload" doc:id="1b7ff757-1a42-4291-b759-50632db842d3" />
        <foreach doc:name="For Each" doc:id="2010c075-b487-4977-8a34-0f58adb23a7d" >
            <http:request method="GET" doc:name="Request" doc:id="a0524fe5-2344-46c2-bb03-90660fb8fc04" url='#["http://localhost:8091/" ++ payload]'/>
            <ee:transform doc:name="Transform Message" doc:id="0aca9608-f7a1-4e1a-a246-11830b6f245e" >
                <ee:message >
                </ee:message>
                <ee:variables >
                    <ee:set-variable variableName="result" ><![CDATA[%dw 2.0
output application/json
---
vars.result + payload 
                ]]></ee:set-variable>
                </ee:variables>
            </ee:transform>
            <logger level="INFO" doc:name="payload" doc:id="22bc3348-90f9-4290-bb53-ee4c19af1f18" message="#[payload]"/>
        </foreach>
        <logger level="INFO" doc:name="payload" doc:id="00e70062-6898-42d7-bc50-974f23a4cd93" message="#[payload]"/>
    </flow>
    <flow name="Testing_foreach_stackoverflowFlow1" doc:id="fc055a5c-09a0-4dce-99be-70e863ffb0ff" >
        <http:listener doc:name="Listener" doc:id="298d5fff-c712-4307-895a-7d4faac42eb1" config-ref="api-httpListenerConfig" path="/test1"/>
        <set-payload value='"grapes"' doc:name="Set Payload" doc:id="bfce761e-c63a-4a06-ab9b-f17360d9a7d2" />
    </flow>
    <flow name="Testing_foreach_stackoverflowFlow2" doc:id="12d67768-525e-46b0-91b4-ce9377b0b7d1" >
        <http:listener doc:name="Listener" doc:id="24fc80d9-0f93-4217-a1a2-3802b57ce3cb" config-ref="api-httpListenerConfig" path="/test2"/>
        <set-payload value='"banana"' doc:name="Set Payload" doc:id="01ad0e82-5f50-49a3-9804-10190953b1c0" />
    </flow>
</mule>

输入图片此处描述

Please check the below code which will help you. I am setting the null variable and concatenating the httpRequest response.

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

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <flow name="Testing_foreach_stackoverflowFlow" doc:id="95fe6a04-4aca-4106-a0b3-b53afb7be95a" >
        <scheduler doc:name="Scheduler" doc:id="e6a81b1c-43f9-486a-a451-b31bcadbe23f" >
            <scheduling-strategy >
                <fixed-frequency frequency="5" timeUnit="MINUTES"/>
            </scheduling-strategy>
        </scheduler>
        <set-variable value="#[%dw 2.0



output application/json



---



[]]" doc:name="Set Variable" doc:id="7b1bbba6-d63d-4f89-905b-8b9c7d6a7555" variableName="result"/>
        <set-payload value='#[["test1","test2"]]' doc:name="Set Payload" doc:id="1b7ff757-1a42-4291-b759-50632db842d3" />
        <foreach doc:name="For Each" doc:id="2010c075-b487-4977-8a34-0f58adb23a7d" >
            <http:request method="GET" doc:name="Request" doc:id="a0524fe5-2344-46c2-bb03-90660fb8fc04" url='#["http://localhost:8091/" ++ payload]'/>
            <ee:transform doc:name="Transform Message" doc:id="0aca9608-f7a1-4e1a-a246-11830b6f245e" >
                <ee:message >
                </ee:message>
                <ee:variables >
                    <ee:set-variable variableName="result" ><![CDATA[%dw 2.0
output application/json
---
vars.result + payload 
                ]]></ee:set-variable>
                </ee:variables>
            </ee:transform>
            <logger level="INFO" doc:name="payload" doc:id="22bc3348-90f9-4290-bb53-ee4c19af1f18" message="#[payload]"/>
        </foreach>
        <logger level="INFO" doc:name="payload" doc:id="00e70062-6898-42d7-bc50-974f23a4cd93" message="#[payload]"/>
    </flow>
    <flow name="Testing_foreach_stackoverflowFlow1" doc:id="fc055a5c-09a0-4dce-99be-70e863ffb0ff" >
        <http:listener doc:name="Listener" doc:id="298d5fff-c712-4307-895a-7d4faac42eb1" config-ref="api-httpListenerConfig" path="/test1"/>
        <set-payload value='"grapes"' doc:name="Set Payload" doc:id="bfce761e-c63a-4a06-ab9b-f17360d9a7d2" />
    </flow>
    <flow name="Testing_foreach_stackoverflowFlow2" doc:id="12d67768-525e-46b0-91b4-ce9377b0b7d1" >
        <http:listener doc:name="Listener" doc:id="24fc80d9-0f93-4217-a1a2-3802b57ce3cb" config-ref="api-httpListenerConfig" path="/test2"/>
        <set-payload value='"banana"' doc:name="Set Payload" doc:id="01ad0e82-5f50-49a3-9804-10190953b1c0" />
    </flow>
</mule>

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文