使用 DataWeave 合并两个有效载荷
Club two payloads with DataWeave
我是 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."
}
},
.
.
.
]
首先使用键过滤适用于负载上每个元素的规则列表。我使用了 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."
}
}
]
我是 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."
}
},
.
.
.
]
首先使用键过滤适用于负载上每个元素的规则列表。我使用了 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."
}
}
]