如何在 mule 4 中转置 csv?
how to transpose csv in mule 4?
我有这样的 csv 负载
id,header,value
1,addr1,hyd
1,addr2,blr
2,addr1,rpr
2,addr2,knk
2,addr3,jpn
要求是将输出负载转换成这个
id,addr1,addr2,addr3
1,hyd,blr,
2,rpr,knk,jpn
我尝试了几种涉及 groupBy 的方法,但无法获得所需的输出。这就是我的 groupBy payload groupBy (item) -> item.id
有人可以帮帮我吗?提前致谢。
%dw 2.0
output application/csv
// Get the distinct headers, maybe you want them ordered too
var headers = "id" >> (payload.*header distinctBy $ orderBy $)
// Get the rows but organize them so that you have objects {header: value}
var rows = payload groupBy $.id
mapObject (
{($$):
$ reduce (e, acc={id: $$}) -> acc ++ {(e.header): e.value}
}
)
pluck $
---
// Iterate over the rows
rows map (
// Iterate over the headers and add the values
headers reduce (e, acc={}) -> acc ++ {(e): $[e]}
)
我有这样的 csv 负载
id,header,value
1,addr1,hyd
1,addr2,blr
2,addr1,rpr
2,addr2,knk
2,addr3,jpn
要求是将输出负载转换成这个
id,addr1,addr2,addr3
1,hyd,blr,
2,rpr,knk,jpn
我尝试了几种涉及 groupBy 的方法,但无法获得所需的输出。这就是我的 groupBy payload groupBy (item) -> item.id
有人可以帮帮我吗?提前致谢。
%dw 2.0
output application/csv
// Get the distinct headers, maybe you want them ordered too
var headers = "id" >> (payload.*header distinctBy $ orderBy $)
// Get the rows but organize them so that you have objects {header: value}
var rows = payload groupBy $.id
mapObject (
{($$):
$ reduce (e, acc={id: $$}) -> acc ++ {(e.header): e.value}
}
)
pluck $
---
// Iterate over the rows
rows map (
// Iterate over the headers and add the values
headers reduce (e, acc={}) -> acc ++ {(e): $[e]}
)