如何在Mule Dataweave中循环并合并为一个 [英] How to loop and combine as one in Mule Dataweave

查看:123
本文介绍了如何在Mule Dataweave中循环并合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有json的请求,并且期望响应如下所述.它需要groupBy clientItemCode并且我在某个地方停留在相同的地方循环播放.同时使用MapObjectreduce功能组合.任何帮助将不胜感激.

I have request of json and expected response one as mentioned below. It need to groupBy clientItemCode and i'm halfway somewhere stuck to loop around in same. Used both MapObject and reduce combination of function. Any help will be appreciated.

[
{
  "ClientCode": "1",
  "ClientItemCode": "245",
  "LocationId": "CLOSED"
 },
 {
  "ClientCode": "1",
  "ClientItemCode": "245",
  "LocationId": "OPEN"
 },
    {
  "ClientCode": "2",
  "ClientItemCode": "245",
  "LocationId": "CHECKOUT"
 },
 {
  "ClientCode": "2",
  "ClientItemCode": "245",
  "LocationId": "TEST"
 },
 {
  "ClientCode": "1",
  "ClientItemCode": "123",
  "LocationId": "OPEN"
 },
 {
  "ClientCode": "1",
  "ClientItemCode": "123",
  "LocationId": "CLOSED"
 }
 ]

预期的响应:

  <Results>
  <Result>
    <ClientItemCode>123<ClientItemCode>
    <ResultLines>
      <ResultLine>
        <ClientCode>1</ClientCode>
        <From>
          <LocationId>OPEN</LocationId>
        </From>
        <To>
          <LocationId>CLOSED</LocationId>
        </To>
      </ResultLine>
       <ResultLine>
        <ClientCode>2</ClientCode>
        <From>
          <LocationId>CHECKOUT</LocationId>
        </From>
        <To>
          <LocationId>TEST</LocationId>
        </To>
      </ResultLine>
    </ResultLines>
  </Result>
  <Result>
   <CientItemCode>245<ClientItemCode>
   <ResultLines>
      <ResultLine>
        <ClientCode>1</ClientCode>
        <From>
          <LocationId>CLOSED</LocationId>
        </From>
        <To>
          <LocationId>OPEN</LocationId>
        </To>
     </ResultLine>
    </ResultLines>
  </Result>
</Results>

推荐答案

这是我想出的,如果我有更多时间投入,可能可以稍微简化一下.试试看:

This is what I came up with, probably it could be simplified a little had I had more time to devote. Give it a try:

%dw 2.0
output application/xml
var data = [
 {
  "ClientCode": "1",
  "ClientItemCode": "245",
  "LocationId": "CLOSED"
 },
 {
  "ClientCode": "1",
  "ClientItemCode": "245",
  "LocationId": "OPEN"
 },
    {
  "ClientCode": "2",
  "ClientItemCode": "245",
  "LocationId": "CHECKOUT"
 },
 {
  "ClientCode": "2",
  "ClientItemCode": "245",
  "LocationId": "TEST"
 },
 {
  "ClientCode": "1",
  "ClientItemCode": "123",
  "LocationId": "OPEN"
 },
 {
  "ClientCode": "1",
  "ClientItemCode": "123",
  "LocationId": "CLOSED"
 }
]
---
// I assume that your data are ordered and all the records that will be From and To
// are paired with one another. It is doable without making such assumption but the
// algorithm will get complex.
results: do {
    // Group by the data
    var groupedData = data map {($),(From: true) if (isEven($$))} groupBy $.ClientItemCode
    // Order the client Ids
    var orderedClientIds = groupedData pluck $$ orderBy $ as Number
    ---
    orderedClientIds reduce (cId, results={}) -> do {
        var clientItemCode = cId
        var groupedByClientICode = groupedData[cId] groupBy $.ClientCode pluck $
        ---
        results ++ {result: {
            ClientItemCode: clientItemCode,
            ResultLines: groupedByClientICode reduce (cliCode, lines={}) -> do {
                var clientCode = cliCode[0].ClientCode
                ---
                lines ++ {
                    ClientCode: clientCode,
                    ResultLine: cliCode reduce (e, acc={}) -> do {
                        var locRec = {LocationId: e.LocationId}
                        ---
                        acc ++ (if (e.From?) {From: locRec } else {To: locRec})
                    }
                }
            }
        }}
    }
}

我在评论中也做了一个假设:我假设您的数据是有序的,并且所有将要成为FromTo的记录都是相互配对的.

I also made an assumption as I replicate in the comments: I assume that your data are ordered and all the records that will be From and To are paired with one another.

再次编辑代码以强制对ClientItemCode进行排序,然后在创建result标记的所有转换之前按顺序访问每个值.其余代码与以前几乎相同.不知道为什么简单的orderBy对您不起作用,对我却不起作用.

Edited the code yet again in order to force the sorting of the ClientItemCode and then access each one of the values in order before all the transformations in creating result tags. The rest of the code is almost the same as before. Not sure why a simple orderBy did not work for you, it did for me.

这篇关于如何在Mule Dataweave中循环并合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆