【问题标题】:"Merge two payload into one in transform component "“在转换组件中将两个有效载荷合并为一个”
【发布时间】:2019-05-13 06:09:08
【问题描述】:

我正在尝试将两个 json 有效负载添加到一个中,但出现错误。我正在使用分散收集来获取多个有效负载并将有效负载组合成转换消息。 错误是:执行时出现异常: 有效载荷 [0] 映射 (bookListing) -> 使用 (id = bookListing.bookId) { “地图”运算符的类型不匹配 找到:字符串,:函数 需要:数组,:函数。

代码:

      <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<file:connector name="file-connector-config" autoDelete="false" streaming="true" validateConnections="true" doc:name="File" />
<flow name="muleRequester">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/requester" doc:name="HTTP"/>
    <scatter-gather doc:name="Scatter-Gather">
        <processor-chain>
            <mulerequester:request resource="file://src/main/resources/input1" returnClass="java.lang.String" doc:name="Retrieve File1"/>
            <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        </processor-chain>
        <processor-chain>
            <mulerequester:request resource="file://src/main/resources/input2" returnClass="java.lang.String" doc:name="Retrieve File2"/>
            <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        </processor-chain>
    </scatter-gather>
    <dw:transform-message doc:name="Transform Message">
        <dw:set-payload><![CDATA[%dw 1.0
         %output application/json
         ---
         payload[0]  map (bookListing) -> using (id = bookListing.bookId) {
bookId: id,
title:  bookListing.title,
price:  bookListing.price,
(payload[1] filter ($.*bookId contains id)  map (bookAuthor) -> {
    author:bookAuthor.author
        })
    }]]></dw:set-payload>
     </dw:transform-message>
     <file:outbound-endpoint responseTimeout="10000" doc:name="File" outputPattern="#[function:systime].json" path="src/main/resources/output"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>

有效载荷 1 - [ { "bookId":"101", "title":"世界历史", “价格”:“19.99” }, { "bookId":"202", "title":"伟大的户外活动", “价格”:“15.99” } ]

有效载荷 2- [ { "bookId":"101", “作者”:“约翰·多伊” }, { "bookId":"202", “作者”:“简·多伊” } ]

预期输出-

[ { "bookId": "101", "title": "世界历史", “年份”:“2017”, “isbn”:“11111”, “价格”:“19.99”, “作者”:“约翰·多伊” }, { "bookId": "202", "title": "伟大的户外活动", “年”:“2016”, "isbn": "22222", “价格”:“15.99”, “作者”:“简·多伊” } ]

【问题讨论】:

    标签: mule mule-studio mule-component mule-el mule-esb


    【解决方案1】:

    从文件返回的值只是字符串,因此需要先将它们读入 dataweave 可以使用的东西。像这样使用读取函数:

    read(payload[0], 'application/json')
    

    read(payload[1], 'application/json')
    

    这是一个完整的例子。 (我已经在 scatter-gather 中删除了示例 json):

        <scatter-gather doc:name="Scatter-Gather">
           <set-payload value="[ { &quot;bookId&quot;:&quot;101&quot;, &quot;title&quot;:&quot;world history&quot;, &quot;price&quot;:&quot;19.99&quot; }, { &quot;bookId&quot;:&quot;202&quot;, &quot;title&quot;:&quot;the great outdoors&quot;, &quot;price&quot;:&quot;15.99&quot; } ]" doc:name="payload1"/>
           <set-payload value="[ { &quot;bookId&quot;:&quot;101&quot;, &quot;author&quot;:&quot;john doe&quot; }, { &quot;bookId&quot;:&quot;202&quot;, &quot;author&quot;:&quot;jane doe&quot; } ]" doc:name="payload2"/>
        </scatter-gather>
    
        <dw:transform-message doc:name="Transform Message">
           <dw:set-payload><![CDATA[%dw 1.0
    %output application/json
    ---
    read(payload[0], 'application/json')  map (bookListing) -> using (id = bookListing.bookId) {
    bookId: id,
    title:  bookListing.title,
    price:  bookListing.price,
    (read(payload[1], 'application/json') filter ($.*bookId contains id)  map (bookAuthor) -> {
        author:bookAuthor.author
    })
    }]]></dw:set-payload>
       </dw:transform-message>
    

    或者您可以事先将整个内容读入 Json

    【讨论】:

      猜你喜欢
      • 2014-11-10
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 2012-11-19
      • 1970-01-01
      相关资源
      最近更新 更多