【问题标题】:Club two payloads with DataWeave使用 DataWeave 组合两个有效负载
【发布时间】:2021-10-22 01:59:36
【问题描述】:

我是 MuleSoft 的新手,正在为 DataWeave 的场景苦苦挣扎。有人可以帮助我优化合并两个有效负载以获得所需输出的方法吗? payload1 有字段,payload2 有这些字段的规则集; 我们需要为每个领域制定相应的规则;例如,'name' 字段需要与 'name' 规则合并,类似地,'roll' 字段需要与 'roll' 规则合并(它们可能是单个字段的多个规则);等等:

数据:

有效载荷1 =

    [
        {
            "ID" : "1",
            "Name" : "ABC",
            "Roll" : 123,
            "Address" : "PQR-234",
            "Standard" : "5"
        },
        {
            "ID" : "2",
            "Name" : "PQR",
            "Roll" : 456,
            "Address" : "REC-678",
            "Standard" : "7"
        },.
        .
        .
    ]

有效载荷2 =

    [
        {
            "field" : "Name",
            "field-Type": "String",
            "operator": "Not-In-List" ,
            "condition-Operand-Type": "",
            "error-Message": "Name - Invalid Value: Name not found in the list."
        },
        {
            "field" : "Name",
            "field-Type": "String",
            "operator": "invalid" ,
            "condition-Operand-Type": "",
            "error-Message": "Name - Invalid Value: There cannot be special char in a name."
        },
        {
            "field" : "Roll",
            "field-Type": "String",
            "operator": "Not-In-List" ,
            "condition-Operand-Type": "",
            "error-Message": "Roll - Invalid Value: Roll not found in the list."
        },
        {
            "field" : "Address",
            "field-Type": "String",
            "operator": "Not-In-List" ,
            "condition-Operand-Type": "",
            "error-Message": "Address - Invalid Value: Address not found in the list."
        },
        .
        .
        .
    ]

期望的输出:

    [
        {
            "ID" : "1",
            "Name" : "ABC",
            "data-Quality-Rule" : 
            {
                "field" : "ABC",
                "field-Type": "String",
                "operator": "Not-In-List" ,
                "condition-Operand-Type": "",
                "error-Message": "Name - Invalid Value: Name not found in the list."
            }
        },
        {
            "ID" : "1",
            "Name" : "ABC",
            "data-Quality-Rule" : 
            {
                "field" : "ABC",
                "field-Type": "String",
                "operator": "invalid" ,
                "condition-Operand-Type": "",
                "error-Message": "Name - Invalid Value: There cannot be special char in a name."
            }
        },
        {
            "ID" : "1",
            "Roll" : "123",
            "data-Quality-Rule" : 
            {
                "field" : "123",
                "field-Type": "String",
                "operator": "Not-In-List" ,
                "condition-Operand-Type": "",
                "error-Message": "Roll - Invalid Value: Roll not found in the list."
            }
        },
        .
        .
        .
        
    ]

【问题讨论】:

    标签: dataweave mule4


    【解决方案1】:

    首先使用键过滤适用于有效负载上每个元素的规则列表。我使用了 namesOf() 但也可以使用 pluck() 来完成。然后使用列表中每个项目的规则列表来映射输出,而不是尝试在原始有效负载上执行此操作。并使用 flatMap() 代替 map() 来消除每一步嵌套列表的不便。

    脚本:

    %dw 2.0
    output application/json
    fun applyRules(p, r)=
        namesOf(p) flatMap ((keyName, order) -> rules filter ($.field == keyName as String))
    ---
    payload flatMap ((item, order) -> 
        applyRules(item, vars.rules) 
        map {
                ID: item.ID, 
                Name: item.Name, 
                "data-quality-rules": $
            }
        ) 
    

    有效载荷:

    [
        {
            "ID" : "1",
            "Name" : "ABC",
            "Roll" : 123,
            "Address" : "PQR-234",
            "Standard" : "5"
        },
        {
            "ID" : "2",
            "Name" : "PQR",
            "Roll" : 456,
            "Address" : "REC-678",
            "Standard" : "7"
        }
    ]
    

    变量 vars.rules:

    [
        {
            "field" : "Name",
            "field-Type": "String",
            "operator": "Not-In-List" ,
            "condition-Operand-Type": "",
            "error-Message": "Name - Invalid Value: Name not found in the list."
        },
        {
            "field" : "Name",
            "field-Type": "String",
            "operator": "invalid" ,
            "condition-Operand-Type": "",
            "error-Message": "Name - Invalid Value: There cannot be special char in a name."
        },
        {
            "field" : "Roll",
            "field-Type": "String",
            "operator": "Not-In-List" ,
            "condition-Operand-Type": "",
            "error-Message": "Roll - Invalid Value: Roll not found in the list."
        },
        {
            "field" : "Address",
            "field-Type": "String",
            "operator": "Not-In-List" ,
            "condition-Operand-Type": "",
            "error-Message": "Address - Invalid Value: Address not found in the list."
        }
    ]
    

    输出:

    [
      {
        "ID": "1",
        "Name": "ABC",
        "data-quality-rules": {
          "field": "Name",
          "field-Type": "String",
          "operator": "Not-In-List",
          "condition-Operand-Type": "",
          "error-Message": "Name - Invalid Value: Name not found in the list."
        }
      },
      {
        "ID": "1",
        "Name": "ABC",
        "data-quality-rules": {
          "field": "Name",
          "field-Type": "String",
          "operator": "invalid",
          "condition-Operand-Type": "",
          "error-Message": "Name - Invalid Value: There cannot be special char in a name."
        }
      },
      {
        "ID": "1",
        "Name": "ABC",
        "data-quality-rules": {
          "field": "Roll",
          "field-Type": "String",
          "operator": "Not-In-List",
          "condition-Operand-Type": "",
          "error-Message": "Roll - Invalid Value: Roll not found in the list."
        }
      },
      {
        "ID": "1",
        "Name": "ABC",
        "data-quality-rules": {
          "field": "Address",
          "field-Type": "String",
          "operator": "Not-In-List",
          "condition-Operand-Type": "",
          "error-Message": "Address - Invalid Value: Address not found in the list."
        }
      },
      {
        "ID": "2",
        "Name": "PQR",
        "data-quality-rules": {
          "field": "Name",
          "field-Type": "String",
          "operator": "Not-In-List",
          "condition-Operand-Type": "",
          "error-Message": "Name - Invalid Value: Name not found in the list."
        }
      },
      {
        "ID": "2",
        "Name": "PQR",
        "data-quality-rules": {
          "field": "Name",
          "field-Type": "String",
          "operator": "invalid",
          "condition-Operand-Type": "",
          "error-Message": "Name - Invalid Value: There cannot be special char in a name."
        }
      },
      {
        "ID": "2",
        "Name": "PQR",
        "data-quality-rules": {
          "field": "Roll",
          "field-Type": "String",
          "operator": "Not-In-List",
          "condition-Operand-Type": "",
          "error-Message": "Roll - Invalid Value: Roll not found in the list."
        }
      },
      {
        "ID": "2",
        "Name": "PQR",
        "data-quality-rules": {
          "field": "Address",
          "field-Type": "String",
          "operator": "Not-In-List",
          "condition-Operand-Type": "",
          "error-Message": "Address - Invalid Value: Address not found in the list."
        }
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      相关资源
      最近更新 更多