如何在dataweave 2上以相同的变换进行分组并映射?

问题描述 投票:0回答: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://vertigobr.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://vertigobr.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://vertigobr.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://vertigobr.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://vertigobr.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://vertigobr.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://vertigobr.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://vertigobr.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": [

      ]
    }
  }
]

我不明白为什么这不起作用。我阅读了文档,发现groupBy中的mapdw 2.0dw 1.0几乎相同。

mule-component dataweave mule-esb mulesoft
2个回答
0
投票

根据this问题,有必要在pluck之后添加groupBy,而不添加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: ''
}

0
投票

问题是in DataWeave 1.0 map() accepted an object as an argument,除了数组。在DataWeave 2.0 it is defined only for arrays and null中。您需要遍历groubBy()结果对象中的键。

© www.soinside.com 2019 - 2024. All rights reserved.