【问题标题】:Dataweave script works for preview, fails in actual useDataweave 脚本用于预览,实际使用失败
【发布时间】:2017-01-19 15:27:49
【问题描述】:

使用 Studio 6.1.0

运行时 3.8.2ee

我编写了一个 dataweave 脚本,当我在“Transform Message”窗口中预览结果时工作正常,但是当我实际运行我的应用程序时,我收到一个错误,表明 dataweave 脚本存在问题。

这是错误:

Invalid input "[\n  \t\t({\n  \t\t\teffectiveTerm:
template.effectiveTerms.effectiveFromTerm,\n  \t\t\tattributeCode: "VARU"\n  \t\t} as :object
{ class: "edu.mit.courseleaf.domain.backfill.OldAttribute" })\n  \t\twhen
template.units.variableUnits == "Y",", expected flatten, typeOf, using, unzip, capitalize,
avg, min, lower, upper, max, singularize, not, dasherize, ordinalize, camelize, trim, sizeOf,
value, pluralize, sum or underscore (line 16, column 24)
(java.util.concurrent.ExecutionException). Message payload is of type: HashMap

dataweave 脚本的流程,预览的输入,实际执行的 json 包括在下面。几个cmets:

  1. 错误中引用的 dataweave 脚本部分用于 attributes 属性。这里的目标是将属性设置为 OldAttribute 对象的列表。列表的内容取决于源数据中的两个独立元素:subjectTemplate.Units.variableUnits == "Y" 和 attributes 元素中的值子集。
  2. 我知道我们可以直接从 JSON 转到 POJO。在完整的应用程序中,我们正在对中间 HashMap 进行额外处理,因此我们希望保留两步转换。
  3. edu.mit.courseleaf.domain.backfill.OldAttribute 和 edu.mit.courseleaf.domain.backfill.SourceSubject 是 POJO,具有默认构造函数以及我们正在使用的属性的正确 getter 和 setter。

使用 dataweave 脚本的流程:

<flow name="temp-json-to-map-to-pojo-example-for-so">
    <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
    <set-payload value="#[payload]" mimeType="*/*" doc:name="Set Payload"/>
    <dw:transform-message doc:name="Transform Message">
        <dw:input-payload doc:sample="sample_data/map.dwl"/>
        <dw:set-payload><![CDATA[%dw 1.0
            %input payload application/java
            %output application/java
            %function passThroughAttributes(attributeList)  attributeList - "RPT" - "SWE" - "FSEM"
            ---
            using (template = payload.subjectTemplate
                    , attributes = payload.subjectTemplate.attributes
            )
            {
                subjectCode:template.mainSubjectCode,
                subjectNumber:template.mainSubjectNumber,
                attributes: flatten [
                    ({
                        effectiveTerm: template.effectiveTerms.effectiveFromTerm,
                        attributeCode: "VARU"
                    } as :object { class: "edu.mit.courseleaf.domain.backfill.OldAttribute" })
                    when template.units.variableUnits == "Y",
                    passThroughAttributes(attributes)   map (
                    {
                        attributeCode: $,
                        effectiveTerm: template.effectiveTerms.effectiveFromTerm
                    }
                        )  as :object { class: "edu.mit.courseleaf.domain.backfill.OldAttribute" }
                ]
            }as :object{class : "edu.mit.courseleaf.domain.backfill.SourceSubject"

        ]]></dw:set-payload>
    </dw:transform-message>
</flow>

用于预览的示例输入:

{
    subjectTemplate: {
        subjectTemplateId: "43C9C99A88B76AFBE053282701126517",
        mainSubjectCode: "HAA",
        mainSubjectNumber: "T101",
        subjectLevel: "U",
        effectiveTerms: {
            effectiveFromTerm:"2017SP",
            effectiveThruTerm: "999999"
        },
        units: {
            variableUnits: "Y",
            lectureUnits: 9,
            labUnits: 3,
            prepUnits: 0,
            totalCreditLimitUnits: 0
        },
        attributes: ["RPT", "XLIS"]
    }

}

显示正确填充的属性属性的预览屏幕截图 流的实际 JSON 输入

{
    "subjectTemplate": {
        "subjectTemplateId": "43C9C99A88B76AFBE053282701126517",
        "mainSubjectCode": "HAA",
        "mainSubjectNumber": "T101",
        "subjectLevel": "U",
        "effectiveTerms": {
            "effectiveFromTerm": "2017SP",
            "effectiveThruTerm": "999999"
        },
        "units": {
            "variableUnits": "Y",
            "lectureUnits": 9,
            "labUnits": 3,
            "prepUnits": 0,
            "totalCreditLimitUnits": 0
        },
        "attributes": ["RPT", "XLIS"]
    }
}

【问题讨论】:

  • IMO,不要依赖预览。

标签: mule dataweave


【解决方案1】:

请尝试在 dataweave 中使用 group by 将所有相同类别的产品分组到一个数组中,然后根据您的要求进行进一步清理。详情可以参考here。

【讨论】:

    猜你喜欢
    • 2015-09-01
    • 1970-01-01
    • 2020-12-04
    • 2017-04-26
    • 2012-04-10
    • 1970-01-01
    • 2017-08-12
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多