Mulesoft集成-优化多个数据的分组

时间:2019-09-10 03:19:01

标签: dataweave mulesoft

使用Dataweave 1.0

有多个数据源进入Mule。 我们合并每个系统的有效负载。 Record_A,Record_B,Record_C和Record_D。

ID用作查找匹配项并将响应分组到预期输出中的键。

问题是它可以正常工作,但是使用当前代码,它基本上遍历所有单独的记录,比较记录集非常高时导致性能问题的情况。 (实际的有效负载也比示例中的有效负载大得多,但具有相同的结构)。

我正在使用过滤器功能来过滤并仅查找匹配的记录。

同样,我确实获得了预期的输出,但性能却不高。

%dw 1.0
%output application/json

%var payload = {
    "PERSON": [
        {
            "ID": "aaaa",
            "firstname": "Homer",
            "lastname": "Simpson"
        },
        {
            "ID": "bbbb",
            "firstname": "Bart",
            "lastname": "Simpson"
        },
        {
            "ID": "cccc",
            "firstname": "Meggie",
            "lastname": "Yolo"
        }
    ],
    "ADDRESS": [
        {
            "ID": "aaaa",
            "address": "1 hello world",
            "postcode": "1234"
        },
        {
            "ID": "bbbb",
            "address": "13 Nuclear Plant",
            "postcode": "3333"
        }
    ],
    "CAR": [
        {
            "ID": "aaaa",
            "brand": "Mercedes",
            "numberplate": "AAA111"
        },
        {
            "ID": "aaaa",
            "brand": "Toyota",
            "numberplate": "BBB322"
        },
        {
            "ID": "bbbb",
            "brand": "Mercedes",
            "numberplate": "ABC123"
        }
    ]
}

---

payload.PERSON map ((item) -> {
    "PERSON": payload.PERSON filter ($.ID contains item.ID) map (items) -> {
        (items)
    },
    ("ADDRESS": payload.ADDRESS filter ($.ID contains item.ID) map (items) -> {
        (items)
    }) when payload.ADDRESS != null,
    ("CAR": payload.CAR filter ($.ID contains item.ID) map (items) -> {
        (items)
    }) when payload.CAR != null
})

预期的输出如下所示。

[
  {
    "PERSON": [
      {
        "ID": "aaaa",
        "firstname": "Homer",
        "lastname": "Simpson"
      }
    ],
    "ADDRESS": [
      {
        "ID": "aaaa",
        "address": "1 hello world",
        "postcode": "1234"
      }
    ],
    "CAR": [
      {
        "ID": "aaaa",
        "brand": "Mercedes",
        "numberplate": "AAA111"
      },
      {
        "ID": "aaaa",
        "brand": "Toyota",
        "numberplate": "BBB322"
      }
    ]
  },
  {
    "PERSON": [
      {
        "ID": "bbbb",
        "firstname": "Bart",
        "lastname": "Simpson"
      }
    ],
    "ADDRESS": [
      {
        "ID": "bbbb",
        "address": "13 Nuclear Plant",
        "postcode": "3333"
      }
    ],
    "CAR": [
      {
        "ID": "bbbb",
        "brand": "Mercedes",
        "numberplate": "ABC123"
      }
    ]
  },
  {
    "PERSON": [
      {
        "ID": "cccc",
        "firstname": "Meggie",
        "lastname": "Yolo"
      }
    ],
    "ADDRESS": [

    ],
    "CAR": [

    ]
  }
]

1 个答案:

答案 0 :(得分:4)

尝试一下。

%dw 1.0
%output application/json

%var PERSON = payload.PERSON 
                    groupBy $.ID
                    mapObject ((value, key) -> (key) : value)


%var ADDRESS = payload.ADDRESS 
                    groupBy $.ID
                    mapObject ((value, key) -> (key) : value)

%var CAR = payload.CAR 
                    groupBy $.ID
                    mapObject ((value, key) -> (key) : value)                    
---
payload.PERSON map ((item) -> {
    "PERSON":PERSON[item.ID] map ((persons) -> {
        (persons)
    }),
    "ADDRESS": ADDRESS[item.ID] map ((addresses) -> {
        (addresses)
    }) default [],
     "CAR": CAR[item.ID] map ((cars) -> {
        (cars)
    }) default []
})