Mule 4 - 无法将并行 for_each 循环的结果合并到单个 json 中
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如文档提到的,并行 foreach 范围内的变量更改在其外部无法访问。这是有道理的,因为我们正在处理并行性,并且会导致各种并发问题。
并行 foreach 返回每次执行返回的所有消息的列表。每条消息都包含其有效负载。您已经在输出中获得了所有需要的数据。要将其转换为 JSON,您只需要在 foreach 之后进行与此类似的转换:
我现在无法测试它,因此可能需要一些调整。
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:
I can't test it right now so it may need some tweaking.
请检查下面的代码,它会对您有所帮助。我正在设置 null 变量并连接 httpRequest 响应。
Please check the below code which will help you. I am setting the null variable and concatenating the httpRequest response.