问题描述
从两个不同的流中,我得到一个json输出有效负载,以及如何基于一个公共密钥将它们组合在一起。密钥是动态的,因此我想编写一个适用于任何密钥的通用数据编织。
输入1:
[
{
"CustomerID": "123","Name": "Maria","Phone": "030-0074321"
},{
"CustomerID": "56654","Name": "sdf","Phone": "030-6877452"
}
]
输入2:
[
{
"OrderID": "10643","CustomerID": "123","Price": "200"
},{
"OrderID": "10692","Price": "566"
},{
"OrderID": "10702","CustomerID": "56654","Price": "546"
}
]
预期输出:
[
{
"CustomerID":"123","Name":"Maria","Phone":"030-0074321","Details":[
{
"OrderID":"10643","Price":"200"
},{
"OrderID":"10692","Price":"566"
}
]
},{
"CustomerID":"56654","Name":"sdf","Phone":"030-6877452","Details":[
{
"OrderID":"10702","Price":"546"
}
]
}
]
基于公共密钥(在此示例中为CustomerID),我想将两个输入组合在一起。 正如我提到的,所有键(CustomerID,Name,Phone,OrderID,Price)一直都不相同,它们是动态的。
预先感谢
解决方法
这是我很快想到的:
%dw 2.0
output application/dw
var input1 = [
{
"CustomerID": "123","Name": "Maria","Phone": "030-0074321"
},{
"CustomerID": "56654","Name": "sdf","Phone": "030-6877452"
}
]
var input2 = [
{
"OrderID": "10643","CustomerID": "123","Price": "200"
},{
"OrderID": "10692","Price": "566"
},{
"OrderID": "10702","CustomerID": "56654","Price": "546"
}
]
fun combineByKey(in1,in2,k) = do {
var groupedBy = in2 groupBy $[k]
---
in1 map {
($),details: groupedBy[$[k]]
}
}
---
combineByKey(input1,input2,"CustomerID")
//do {
// var groupedBy = input2 groupBy $.CustomerID
// ---
// input1 map {
// ($),// details: groupedBy[$.CustomerID]
// }
//}
从底部注释掉的表达式中可以看到,它并不长,所以我认为您不需要功能恕我直言。
从本质上讲,您只需要知道两个函数groupBy
和map
,然后知道如何使用do {}
创建闭包(也就是本地化的声明),最后知道如何动态访问字段。
我敢打赌,如果我花更多的时间,我应该能够提出更好的功能,但是现在就可以了:)
可能已经有一些内置函数可以执行此操作,但是我不知道。也许有人会指出。
,此 dataweave 转换满足您提到的条件。
%dw 2.0
output application/json
---
input1 map(value) -> using (id = value.CustomerID)
{
CustomerID: value.CustomerID,Name: value.Name,Phone:value.Phone,Details: (input2 filter ($.*'CustomerID' contains id) map ($ mapObject (k,v) ->{
(v):k
} - "CustomerID"))
}
,
也许您也可以使CommonKey成为动态的。我知道这可能是一个过大的杀伤力,但是我在这里就把它排除在外,以防它以任何可能的方式有所帮助。
DataGrid
,
您可以通过使用一些递归函数或仅通过获取键然后在 in1 和 in2 之间进行比较,然后进行重组以创建 { {1}} 。诀窍是创建一个 primaryKey ,它是所有commonKey的值。
尝试以下脚本。 in1 类似于主要参考,而 in2 是次要参考。要对其进行测试,请尝试在 in2 中添加 foreignKey
字段,并至少使用匹配值 Name
和 Name
来自 CustomerID
in1
,
这是另一个公共密钥为动态的解决方案:
%dw 2.0
output application/json
import * from dw::core::Arrays
var inp1=[
{
"CustomerID": "123","Phone": "030-6877452"
}
]
var inp2=[
{
"OrderID": "10643","Price": "546"
}
]
//get all the keys from both the arrays
var inp1keys=((inp1 reduce(item,acc) -> item ++ acc) pluck $$) distinctBy $
var inp2keys=((inp2 reduce(item,acc) -> item ++ acc) pluck $$) distinctBy $
//get the matching key in the array
var matchingkey=((inp1keys map (v0,k0) ->
{
matched: if(inp2keys contains v0) v0 else null
}.matched) filter $ != null)[0]
---
/*
Steps for the script below:
1.Join both the array on the common key fetched dynamically above.
2.Remove the common key from the rigt part obtained after join.
3.After that merge the Details part for a given common key under a given common key id(achieved with the reduce)
*/
((join(inp1,inp2,(inp1) -> inp1."$(matchingkey)",(inp2) -> inp2."$(matchingkey)"))
map (v0,k0) ->
{
((v0.l) ++ (Details:v0.r - matchingkey))
}) reduce (item,acc) -> if( acc."$(matchingkey)" contains item."$(matchingkey)"[0])
((acc - 'Details') ++ Details:[acc.Details,item.Details])
else [acc] + item