如何在 dataweave 2 上进行 groupBy 和映射相同的转换?
how to groupBy and map at same transform on dataweave 2?
我有这个 dataweave 1.0 script
效果很好:
%dw 1.0
%output application/java
---
flowVars.worklogs groupBy $.author.accountId map {
accountId: $.author.accountId[0],
displayName: $.author.displayName[0],
timeSpentMinutesMonth: (sum $.timeSpentSeconds) / 3600,
billableMinutesMonth: (sum $.billableSeconds) / 3600,
emailAddress: ''
}
但是,现在我正在更新 mule 4
的代码,我无法使这个转换顺利进行。
我已经试过这样更新它了:
%dw 2.0
output application/java
---
vars.worklogs groupBy $.author.accountId map {
accountId: $.author.accountId[0],
displayName: $.author.displayName[0],
timeSpentMinutesMonth: (sum($.timeSpentSeconds)) / 3600,
billableMinutesMonth: (sum($.billableSeconds)) / 3600,
emailAddress: ''
}
但是我得到了这个错误:
org.mule.runtime.core.internal.message.ErrorBuilder$ErrorImplementation
{
description="You called the function 'map' with these arguments:
1: Object ({"5d8b681427fe990dc2d3404a": [{self: "https://api.tempo.io/core/3/worklogs/54...)
2: Function ((v:Any, i:Any) -> ???)
But it expects arguments of these types:
1: Array
2: Function
4| vars.worklogs groupBy $.author.accountId map (v, i) -> {
| ...
10| }
Trace:
at map (line: 4, column: 1)
at main (line: 4, column: 42)" evaluating expression: "%dw 2.0
output application/java
---
vars.worklogs groupBy $.author.accountId map (v, i) -> {
accountId: v.author.accountId[0],
displayName: v.author.displayName[0],
timeSpentMinutesMonth: (sum(v.timeSpentSeconds)) / 3600,
billableMinutesMonth: (sum(v.billableSeconds)) / 3600,
emailAddress: ''
}".
变量worklogs
包含一个json
:
[
{
"self": "https://api.tempo.io/core/3/worklogs/5408",
"tempoWorklogId": 5408,
"jiraWorklogId": 15408,
"issue": {
"self": "https://xpto.atlassian.net/rest/api/2/issue/ABC-123",
"key": "ABC-123",
"id": 11005
},
"timeSpentSeconds": 28800,
"billableSeconds": 28800,
"startDate": "2020-01-31",
"startTime": "00:00:00",
"description": "creating new song",
"createdAt": "2020-02-28T13:30:58Z",
"updatedAt": "2020-02-28T13:30:58Z",
"author": {
"self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5d8b681427fe990dc2d3404a",
"accountId": "5d8b681427fe990dc2d3404a",
"displayName": "john lennon"
},
"attributes": {
"self": "https://api.tempo.io/core/3/worklogs/5408/work-attribute-values",
"values": [
]
}
},
{
"self": "https://api.tempo.io/core/3/worklogs/5166",
"tempoWorklogId": 5166,
"jiraWorklogId": 15166,
"issue": {
"self": "https://xpto.atlassian.net/rest/api/2/issue/CDE-99",
"key": "CDE-99",
"id": 10106
},
"timeSpentSeconds": 3600,
"billableSeconds": 3600,
"startDate": "2020-01-31",
"startTime": "00:00:00",
"description": "call with stakeholders",
"createdAt": "2020-02-10T18:30:03Z",
"updatedAt": "2020-02-10T18:30:03Z",
"author": {
"self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5b27ad3902cfea1ba6411c3f",
"accountId": "5b27ad3902cfea1ba6411c3f",
"displayName": "chandler bing"
},
"attributes": {
"self": "https://api.tempo.io/core/3/worklogs/5166/work-attribute-values",
"values": [
]
}
},
{
"self": "https://api.tempo.io/core/3/worklogs/5165",
"tempoWorklogId": 5165,
"jiraWorklogId": 15165,
"issue": {
"self": "https://xpto.atlassian.net/rest/api/2/issue/CDE-99",
"key": "CDE-99",
"id": 10081
},
"timeSpentSeconds": 3600,
"billableSeconds": 3600,
"startDate": "2020-01-31",
"startTime": "00:00:00",
"description": "planning tulsa work trip",
"createdAt": "2020-02-10T18:29:30Z",
"updatedAt": "2020-02-10T18:29:30Z",
"author": {
"self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5b27ad3902cfea1ba6411c3f",
"accountId": "5b27ad3902cfea1ba6411c3f",
"displayName": "chandler bing"
},
"attributes": {
"self": "https://api.tempo.io/core/3/worklogs/5165/work-attribute-values",
"values": [
]
}
},
{
"self": "https://api.tempo.io/core/3/worklogs/5164",
"tempoWorklogId": 5164,
"jiraWorklogId": 15164,
"issue": {
"self": "https://xpto.atlassian.net/rest/api/2/issue/CDE-99",
"key": "CDE-99",
"id": 10108
},
"timeSpentSeconds": 7200,
"billableSeconds": 7200,
"startDate": "2020-01-31",
"startTime": "00:00:00",
"description": "exporting data to cd-rom",
"createdAt": "2020-02-10T18:29:08Z",
"updatedAt": "2020-02-10T18:29:47Z",
"author": {
"self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5b27ad3902cfea1ba6411c3f",
"accountId": "5b27ad3902cfea1ba6411c3f",
"displayName": "chandler-bing"
},
"attributes": {
"self": "https://api.tempo.io/core/3/worklogs/5164/work-attribute-values",
"values": [
]
}
}
]
我不明白为什么这不起作用。我阅读了文档,发现 dw 2.0
中的 groupBy
和 map
与 dw 1.0
.
的工作原理几乎相同
根据this问题,需要在groupBy
后面加一个pluck
,不能加map
:
%dw 2.0
output application/json
---
vars.worklogs groupBy $.author.accountId pluck {
accountId: $.author.accountId[0],
displayName: $.author.displayName[0],
timeSpentMinutesMonth: (sum($.timeSpentSeconds)) / 3600,
billableMinutesMonth: (sum($.billableSeconds)) / 3600,
emailAddress: ''
}
问题是in DataWeave 1.0 map() accepted an object as an argument, in addition to arrays. In DataWeave 2.0 it is defined only for arrays and null。您需要遍历 groubBy() 的结果对象中的键。
我有这个 dataweave 1.0 script
效果很好:
%dw 1.0
%output application/java
---
flowVars.worklogs groupBy $.author.accountId map {
accountId: $.author.accountId[0],
displayName: $.author.displayName[0],
timeSpentMinutesMonth: (sum $.timeSpentSeconds) / 3600,
billableMinutesMonth: (sum $.billableSeconds) / 3600,
emailAddress: ''
}
但是,现在我正在更新 mule 4
的代码,我无法使这个转换顺利进行。
我已经试过这样更新它了:
%dw 2.0
output application/java
---
vars.worklogs groupBy $.author.accountId map {
accountId: $.author.accountId[0],
displayName: $.author.displayName[0],
timeSpentMinutesMonth: (sum($.timeSpentSeconds)) / 3600,
billableMinutesMonth: (sum($.billableSeconds)) / 3600,
emailAddress: ''
}
但是我得到了这个错误:
org.mule.runtime.core.internal.message.ErrorBuilder$ErrorImplementation
{
description="You called the function 'map' with these arguments:
1: Object ({"5d8b681427fe990dc2d3404a": [{self: "https://api.tempo.io/core/3/worklogs/54...)
2: Function ((v:Any, i:Any) -> ???)
But it expects arguments of these types:
1: Array
2: Function
4| vars.worklogs groupBy $.author.accountId map (v, i) -> {
| ...
10| }
Trace:
at map (line: 4, column: 1)
at main (line: 4, column: 42)" evaluating expression: "%dw 2.0
output application/java
---
vars.worklogs groupBy $.author.accountId map (v, i) -> {
accountId: v.author.accountId[0],
displayName: v.author.displayName[0],
timeSpentMinutesMonth: (sum(v.timeSpentSeconds)) / 3600,
billableMinutesMonth: (sum(v.billableSeconds)) / 3600,
emailAddress: ''
}".
变量worklogs
包含一个json
:
[
{
"self": "https://api.tempo.io/core/3/worklogs/5408",
"tempoWorklogId": 5408,
"jiraWorklogId": 15408,
"issue": {
"self": "https://xpto.atlassian.net/rest/api/2/issue/ABC-123",
"key": "ABC-123",
"id": 11005
},
"timeSpentSeconds": 28800,
"billableSeconds": 28800,
"startDate": "2020-01-31",
"startTime": "00:00:00",
"description": "creating new song",
"createdAt": "2020-02-28T13:30:58Z",
"updatedAt": "2020-02-28T13:30:58Z",
"author": {
"self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5d8b681427fe990dc2d3404a",
"accountId": "5d8b681427fe990dc2d3404a",
"displayName": "john lennon"
},
"attributes": {
"self": "https://api.tempo.io/core/3/worklogs/5408/work-attribute-values",
"values": [
]
}
},
{
"self": "https://api.tempo.io/core/3/worklogs/5166",
"tempoWorklogId": 5166,
"jiraWorklogId": 15166,
"issue": {
"self": "https://xpto.atlassian.net/rest/api/2/issue/CDE-99",
"key": "CDE-99",
"id": 10106
},
"timeSpentSeconds": 3600,
"billableSeconds": 3600,
"startDate": "2020-01-31",
"startTime": "00:00:00",
"description": "call with stakeholders",
"createdAt": "2020-02-10T18:30:03Z",
"updatedAt": "2020-02-10T18:30:03Z",
"author": {
"self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5b27ad3902cfea1ba6411c3f",
"accountId": "5b27ad3902cfea1ba6411c3f",
"displayName": "chandler bing"
},
"attributes": {
"self": "https://api.tempo.io/core/3/worklogs/5166/work-attribute-values",
"values": [
]
}
},
{
"self": "https://api.tempo.io/core/3/worklogs/5165",
"tempoWorklogId": 5165,
"jiraWorklogId": 15165,
"issue": {
"self": "https://xpto.atlassian.net/rest/api/2/issue/CDE-99",
"key": "CDE-99",
"id": 10081
},
"timeSpentSeconds": 3600,
"billableSeconds": 3600,
"startDate": "2020-01-31",
"startTime": "00:00:00",
"description": "planning tulsa work trip",
"createdAt": "2020-02-10T18:29:30Z",
"updatedAt": "2020-02-10T18:29:30Z",
"author": {
"self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5b27ad3902cfea1ba6411c3f",
"accountId": "5b27ad3902cfea1ba6411c3f",
"displayName": "chandler bing"
},
"attributes": {
"self": "https://api.tempo.io/core/3/worklogs/5165/work-attribute-values",
"values": [
]
}
},
{
"self": "https://api.tempo.io/core/3/worklogs/5164",
"tempoWorklogId": 5164,
"jiraWorklogId": 15164,
"issue": {
"self": "https://xpto.atlassian.net/rest/api/2/issue/CDE-99",
"key": "CDE-99",
"id": 10108
},
"timeSpentSeconds": 7200,
"billableSeconds": 7200,
"startDate": "2020-01-31",
"startTime": "00:00:00",
"description": "exporting data to cd-rom",
"createdAt": "2020-02-10T18:29:08Z",
"updatedAt": "2020-02-10T18:29:47Z",
"author": {
"self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5b27ad3902cfea1ba6411c3f",
"accountId": "5b27ad3902cfea1ba6411c3f",
"displayName": "chandler-bing"
},
"attributes": {
"self": "https://api.tempo.io/core/3/worklogs/5164/work-attribute-values",
"values": [
]
}
}
]
我不明白为什么这不起作用。我阅读了文档,发现 dw 2.0
中的 groupBy
和 map
与 dw 1.0
.
根据this问题,需要在groupBy
后面加一个pluck
,不能加map
:
%dw 2.0
output application/json
---
vars.worklogs groupBy $.author.accountId pluck {
accountId: $.author.accountId[0],
displayName: $.author.displayName[0],
timeSpentMinutesMonth: (sum($.timeSpentSeconds)) / 3600,
billableMinutesMonth: (sum($.billableSeconds)) / 3600,
emailAddress: ''
}
问题是in DataWeave 1.0 map() accepted an object as an argument, in addition to arrays. In DataWeave 2.0 it is defined only for arrays and null。您需要遍历 groubBy() 的结果对象中的键。