Dataweave脚本适用于预览,在实际使用中失败

时间:2017-01-17 16:27:53

标签: mule dataweave

使用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包含在下面。几条评论:

  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。
  4. 使用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"]
        }
    }
    

1 个答案:

答案 0 :(得分:0)

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