在Mule ESB中聚合两个有效负载

时间:2017-05-23 09:33:58

标签: json mule dataweave mule-esb

我的骡子代码正在打两张桌子并获得一些细节。 第一个是订单详细信息,我将其存储在流变量中,即订单,另一个数据库返回订单项详细信息,我将其存储在 orderitem 变量中。

我想根据一个条件聚合两个有效负载。每个orderId都有订单商品(存储在flowVars.orderitem中),并将这些订单商品映射到相应的orderID。

flowVars.order值如下所示

In[60]:
df = pd.DataFrame({'column': ['A', 'B', 'C', 'D', 'E', 'F', 'G', '-']})
d = {'A':'cat','B':'rabit', 'C':'octpath','D':'spider','E':'mammoth','F':'snake','G':'starfish'}
df['column'] = df['column'].map(d, na_action='ignore').fillna(df['column'])
df

Out[60]: 
     column
0       cat
1     rabit
2   octpath
3    spider
4   mammoth
5     snake
6  starfish
7         -

flowVars.orderitem值如下所示

[{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}]

预期产出

 [{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}]

这里我需要显示订单的相应订单商品详情。所以基本上我需要结合两个有效载荷。

我尝试使用dataweave但不是运气。

Dataweave代码:

[
  {
    "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"
        }
    ]
  }
]

转换后输出:

%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映射订单商品详细信息,但转换后仍然会得到一些空白值。

任何人都可以帮我实现预期的输出。在此先感谢!!!

1 个答案:

答案 0 :(得分:0)

您需要过滤flowVars.orderitem地图,而不是完整地迭代它,并且只在orderId匹配时打印值。

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

然后,您可以删除所有这些'陈述也是。