如何根据CSV中的第一列在Dataweave中实现分组依据

How to implement group by in Dataweave based on first column in CSV

我有一个传入的 CSV 文件,看起来像这样(请注意第一个字段是常见的 - 这是订单号)

36319602,100,12458,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
36319602,101,12459,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
36319602,102,12457,HARVEY NORMAN, 
36319601,110,12458,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
36319601,111,12459,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
36319601,112,12457,HARVEY NORMAN, 
36319603,110,12458,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
36319603,121,12459,HARVEY NORMAN,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
36319603,132,12457,HARVEY NORMAN, 

这是我当前的 Dataweave 代码

list_of_orders: {
    order: payload map ((payload01 , indexOfPayload01) -> {
        order_dtl: 
        [{
            seq_nbr: payload01[1],
            route_nbr: payload01[2]
        }],
        order_hdr: {
            ord_nbr: payload01[0],
            company: payload01[3],
            city: payload01[4],
        }
    })
}

所需输出的示例如下所示......(这只是模拟)。请注意,我希望单个 header 按第一列(订单号)分组 - 但有多个详细信息行


    "list_of_orders": {
      "order": [
        {
          "order_dtl": [
            {
                seq_nbr: 100,
                route_nbr: 12458
            },
            {
                seq_nbr: 101,
                route_nbr: 12459
            },
            {
                seq_nbr: 102,
                route_nbr: 12457
            }                        
          ],
          "order_hdr": 
          {
            ord_nbr: 36319602,
            company: HARVEY NORMAN
          }
        }
      ]
    }

除了重复 order_hdr 键外,它工作正常。 他们想要的是一个 header 键,下面有多个详细信息。 分组将基于 "ord_nbr: payload01[0]"

感谢任何帮助

谢谢

我认为您使用的是 Dataweave 1。在 dw1 中,此 groupBy 获得了所需的输出(请注意,您可以将字段指针 [0]、1 等更改为字段名称映射,如果您将它们设置为元数据等):

%dw 1.0

%output application/json
---
list_of_orders:  {
    order: (payload groupBy ($[0])) map {
    order_dtl: $ map {
        seq_nbr: $[1],
        route_nbr: $[2]
    },
    order_hdr: 
          {
            ord_nbr:  $[0][0],
            company: $[0][3]
          }
 }}

更新

这是具有多个订单的新输入样本的输出:

{
  "list_of_orders": {
    "order": [
      {
        "order_dtl": [
          {
            "seq_nbr": "110",
            "route_nbr": "12458"
          },
          {
            "seq_nbr": "121",
            "route_nbr": "12459"
          },
          {
            "seq_nbr": "132",
            "route_nbr": "12457"
          }
        ],
        "order_hdr": {
          "ord_nbr": "36319603",
          "company": "HARVEY NORMAN"
        }
      },
      {
        "order_dtl": [
          {
            "seq_nbr": "100",
            "route_nbr": "12458"
          },
          {
            "seq_nbr": "101",
            "route_nbr": "12459"
          },
          {
            "seq_nbr": "102",
            "route_nbr": "12457"
          }
        ],
        "order_hdr": {
          "ord_nbr": "36319602",
          "company": "HARVEY NORMAN"
        }
      },
      {
        "order_dtl": [
          {
            "seq_nbr": "110",
            "route_nbr": "12458"
          },
          {
            "seq_nbr": "111",
            "route_nbr": "12459"
          },
          {
            "seq_nbr": "112",
            "route_nbr": "12457"
          }
        ],
        "order_hdr": {
          "ord_nbr": "36319601",
          "company": "HARVEY NORMAN"
        }
      }
    ]
  }
}