本文介绍了在Mule ESB中聚合两个有效负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的m子代码打了两个表并获得了一些详细信息.第一个是订单明细,我将其存储在流变量即 order 中,另一个数据库将返回订单明细,并将其存储在 orderitem 变量中.

My mule code is hitting two tables and getting some details.First one is order details, which I am storing in a flow variable i.e order and another database is returning order item details which I am storing in orderitem variable.

我想基于一个条件来汇总两个有效负载.每个orderId都有订单项(存储在flowVars.orderitem中),并将这些订单项映射到各自的orderID.

I want to aggregate both the payload based on one condition. Every orderId has order items (which is stored in flowVars.orderitem) and map these order items to respective orderID.

flowVars.order值如下所示

[{partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=12345, orderDate=2017-02-28 16:41:41.0, id=22}, {partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=123456, orderDate=2017-02-28 16:41:41.0, id=23}, {partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=11111, orderDate=2017-02-28 16:41:41.0, id=24}, {partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=321123, orderDate=2017-05-19 15:25:41.0, id=26}]

flowVars.orderitem值如下所示

 [{productCode=ELT-LP-ICND1-020067, orderId=12345, quantity=10, id=14}, {productCode=ELT-IP-ICND1-1.0, orderId=12345, quantity=11, id=15}, {productCode=ELT-LP-ICND1-020067, orderId=123456, quantity=12, id=16}, {productCode=ELT-IP-ICND1-1.0, orderId=123456, quantity=13, id=17}, {productCode=ELT-LP-ICND1-020067, orderId=11111, quantity=14, id=18}, {productCode=ELT-IP-ICND1-1.0, orderId=11111, quantity=15, id=19}, {productCode=ELT-LP-ICND2-020067, orderId=321123, quantity=5, id=20}]

预期输出

[
  {
    "orderId": "12345",
    "orderDate": "2017-02-28T16:41:41",
    "partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderItems": [
       {
          "productCode": "ELT-LP-ICND1-020067",
          "quantity": "10"
        },
        {
          "productCode": "ELT-IP-ICND1-1.0",
          "quantity": "11"
        }
    ]
  },
  {
    "orderId": "123456",
    "orderDate": "2017-02-28T16:41:41",
    "partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderItems": [
        {
          "productCode": "ELT-LP-ICND1-020067",
          "quantity": "12"
        },
        {
          "productCode": "ELT-IP-ICND1-1.0",
          "quantity": "13"
        }
    ]
  },
  {
    "orderId": "11111",
    "orderDate": "2017-02-28T16:41:41",
    "partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderItems": [
       {
          "productCode": "ELT-LP-ICND1-020067",
          "quantity": "14"
        },
        {
          "productCode": "ELT-IP-ICND1-1.0",
          "quantity": "15"
        }
    ]
  },
  {
    "orderId": "321123",
    "orderDate": "2017-05-19T15:25:41",
    "partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderItems": [
      {
          "productCode": "ELT-LP-ICND1-020067",
          "quantity": "5"
        }
    ]
  }
]

在这里,我需要显示订单的各个订单商品详细信息.所以基本上我需要结合两个有效载荷.

Here I need to show respective order item details of an order. So basically I need to combine both the payloads.

我尝试使用dataweave,但没有运气.

I tried using dataweave but not luck.

数据编织代码:

%dw 1.0
%output application/json
%var mergeddata = flowVars.orderitem groupBy $.orderId  
---
 flowVars.order map ((data,index) ->
     {
    orderid: data.orderId,
    partnerid: data.partnerId,
    orderdate: data.orderDate,
    order: flowVars.orderitem default [] map ((data1 ,indexOf)  ->
           {
            (productcode: data1.productCode) when (data1.orderId == data.orderId),      
            (quantity: data1.quantity) when (data1.orderId == data.orderId) ,
            (id: data1.id) when (data1.orderId == data.orderId) 
            }

            )})  

转换后输出:

{
    "orderid": "12345",
    "partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderdate": "2017-02-28T16:41:41",
    "order": [
      {
        "productcode": "ELT-LP-ICND1-020067",
        "quantity": 10,
        "id": 14
      },
      {
        "productcode": "ELT-IP-ICND1-1.0",
        "quantity": 11,
        "id": 15
      },
      {

      },
      {

      },
      {

      },
      {

      },
      {

      }
    ]
  },
  {
    "orderid": "123456",
    "partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderdate": "2017-02-28T16:41:41",
    "order": [
      {

      },
      {

      },
      {
        "productcode": "ELT-LP-ICND1-020067",
        "quantity": 12,
        "id": 16
      },
      {
        "productcode": "ELT-IP-ICND1-1.0",
        "quantity": 13,
        "id": 17
      },
      {

      },
      {

      },
      {

      }
    ]
  },
  {
    "orderid": "11111",
    "partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderdate": "2017-02-28T16:41:41",
    "order": [
      {

      },
      {

      },
      {

      },
      {

      },
      {
        "productcode": "ELT-LP-ICND1-020067",
        "quantity": 14,
        "id": 18
      },
      {
        "productcode": "ELT-IP-ICND1-1.0",
        "quantity": 15,
        "id": 19
      },
      {

      }
    ]
  },
  {
    "orderid": "321123",
    "partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
    "orderdate": "2017-05-19T15:25:41",
    "order": [
      {

      },
      {

      },
      {

      },
      {

      },
      {

      },
      {

      },
      {
        "productcode": "ELT-LP-ICND2-020067",
        "quantity": 5,
        "id": 20
      }
    ]
  }
]

如您所见,我快到了,并且能够使用相应的orderId映射订单商品详细信息,但在转换后我仍然得到一些空白值.

As you can see that I am almost there and able to map order item details with respective orderId but still I am getting some blank values after transformation.

任何人都可以帮助我实现预期的输出.预先谢谢!!!

Can anyone help me to achieve expected output. Thanks in advance!!!

推荐答案

您需要过滤flowVars.orderitem映射,而不是完全对其进行迭代,并且仅在orderId匹配时才打印值.

You need to filter the flowVars.orderitem map, rather than iterate it in full and only print values when the orderId matches.

    order: ((flowVars.orderitem default []) filter (data.orderId == $.orderId)) map ((data1 ,indexOf) -> {
            productcode: data1.productCode,      
            quantity: data1.quantity
            id: data1.id
    })

然后,您也可以删除所有这些"when"语句.

You can then remove all of those 'when' statements too.

这篇关于在Mule ESB中聚合两个有效负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 15:05