Deep Dive Into DataWeave Core Arrays Module: Part II
This article explores various types of
Join the DZone community and get the full member experience.
Join For FreeIn the last article, we saw various DW Core Arrays helper functions like some, every, sumBy, and countBy. Here we are going to see various types of joins provided by the DW Core Arrays module.
join combines8 two arrays of objects by a given ID criteria. There are three types of joins provided by the MuleSoft DW Core Arrays Module.
- join
- leftJoin
- outerJoin
We will be using below employee and department payload to understand the joins.
Employee
x
[{
"employeeId":1,
"deptId":1,
"employeeName": "James Thomas"
},
{
"employeeId":2,
"deptId":3,
"employeeName": "James Peter"
},
{
"employeeId":3,
"deptId":3,
"employeeName": "Jackie Chen"
},
{
"employeeId":4,
"deptId":4,
"employeeName": "Jackie Chen"
}]
Department
x
[{
"deptId":"1",
"deptName":"Science"
},
{
"deptId":"2",
"deptName":"Information Technology"
},
{
"deptId":"3",
"deptName":"Physics"
}]
Each Employee is associated with one department.
join
join
returns an array of all the left
items, merged by ID with any right items that exist.
It takes four parameters:
- left — The left side array of the object.
- right — The right side array of the object.
- leftCriteria — The criteria is used to extract ID of the left collection.
- rightCriteria — The criteria is used to extract ID of the right collection.
x
%dw 2.0
output application/json
import * from dw::core::Arrays
var employee=
[{
"employeeId":1,
"deptId":1,
"employeeName": "James Thomas"
},
{
"employeeId":2,
"deptId":3,
"employeeName": "James Peter"
},
{
"employeeId":3,
"deptId":3,
"employeeName": "Jackie Chen"
},
{
"employeeId":4,
"deptId":4,
"employeeName": "Jackie Chen"
}]
var department=
[{
"deptId":"1",
"deptName":"Science"
},
{
"deptId":"2",
"deptName":"Information Technology"
},
{
"deptId":"3",
"deptName":"Physics"
}]
---
join(employee,department,(emp)->(emp.deptId),(dept)->(dept.deptId))
Output
x
[
{
"l": {
"employeeId": 1,
"deptId": 1,
"employeeName": "James Thomas"
},
"r": {
"deptId": "1",
"deptName": "Science"
}
},
{
"l": {
"employeeId": 2,
"deptId": 3,
"employeeName": "James Peter"
},
"r": {
"deptId": "3",
"deptName": "Physics"
}
},
{
"l": {
"employeeId": 3,
"deptId": 3,
"employeeName": "Jackie Chen"
},
"r": {
"deptId": "3",
"deptName": "Physics"
}
}
]
leftJoin
leftJoin
returns an array of all the left
items, merged by ID with any right items that meet the joining criteria.
It takes four parameters:
- left — The left side array of the object.
- right — The right side array of the object.
- leftCriteria — The criteria is used to extract ID of left collection.
- rightCriteria — The criteria is used to extract ID of right collection.
x
%dw 2.0
output application/json
import * from dw::core::Arrays
var employee=
[{
"employeeId":1,
"deptId":1,
"employeeName": "James Thomas"
},
{
"employeeId":2,
"deptId":3,
"employeeName": "James Peter"
},
{
"employeeId":3,
"deptId":3,
"employeeName": "Jackie Chen"
},
{
"employeeId":4,
"deptId":4,
"employeeName": "Jackie Chen"
}]
var department=
[{
"deptId":"1",
"deptName":"Science"
},
{
"deptId":"2",
"deptName":"Information Technology"
},
{
"deptId":"3",
"deptName":"Physics"
}]
---
leftJoin(employee,department,(emp)->(emp.deptId),(dept)->(dept.deptId))-
Output
xxxxxxxxxx
[
{
"l": {
"employeeId": 1,
"deptId": 1,
"employeeName": "James Thomas"
},
"r": {
"deptId": "1",
"deptName": "Science"
}
},
{
"l": {
"employeeId": 2,
"deptId": 3,
"employeeName": "James Peter"
},
"r": {
"deptId": "3",
"deptName": "Physics"
}
},
{
"l": {
"employeeId": 3,
"deptId": 3,
"employeeName": "Jackie Chen"
},
"r": {
"deptId": "3",
"deptName": "Physics"
}
},
{
"l": {
"employeeId": 4,
"deptId": 4,
"employeeName": "Jackie Chen"
}
}
]
outerJoin
outerJoin
returns an array with all the left
items, merged by ID with the right
items in cases where any exist, and it returns right
items that are not present in the left
.
It takes four parameters.
- left — The left side array of the object.
- right — The right side array of the object.
- leftCriteria — The criteria is used to extract ID of left collection.
- rightCriteria — The criteria is used to extract ID of right collection.
x
%dw 2.0
output application/json
import * from dw::core::Arrays
var employee=
[{
"employeeId":1,
"deptId":1,
"employeeName": "James Thomas"
},
{
"employeeId":2,
"deptId":3,
"employeeName": "James Peter"
},
{
"employeeId":3,
"deptId":3,
"employeeName": "Jackie Chen"
},
{
"employeeId":4,
"deptId":4,
"employeeName": "Jackie Chen"
}]
var department=
[{
"deptId":"1",
"deptName":"Science"
},
{
"deptId":"2",
"deptName":"Information Technology"
},
{
"deptId":"3",
"deptName":"Physics"
}]
---
outerJoin(employee,department,(emp)->(emp.deptId),(dept)->(dept.deptId))
Output
xxxxxxxxxx
[
{
"l": {
"employeeId": 1,
"deptId": 1,
"employeeName": "James Thomas"
},
"r": {
"deptId": "1",
"deptName": "Science"
}
},
{
"l": {
"employeeId": 2,
"deptId": 3,
"employeeName": "James Peter"
},
"r": {
"deptId": "3",
"deptName": "Physics"
}
},
{
"l": {
"employeeId": 3,
"deptId": 3,
"employeeName": "Jackie Chen"
},
"r": {
"deptId": "3",
"deptName": "Physics"
}
},
{
"l": {
"employeeId": 4,
"deptId": 4,
"employeeName": "Jackie Chen"
}
},
{
"r": {
"deptId": "2",
"deptName": "Information Technology"
}
}
]
There is no direct rightJoin but you can still achieve using leftJoin. In the leftJoin, you can bring a department in the left and an employee in the right as shown below:
x
%dw 2.0
output application/json
import * from dw::core::Arrays
var employee=
[{
"employeeId":1,
"deptId":1,
"employeeName": "James Thomas"
},
{
"employeeId":2,
"deptId":3,
"employeeName": "James Peter"
},
{
"employeeId":3,
"deptId":3,
"employeeName": "Jackie Chen"
},
{
"employeeId":4,
"deptId":4,
"employeeName": "Jackie Chen"
}]
var department=
[{
"deptId":"1",
"deptName":"Science"
},
{
"deptId":"2",
"deptName":"Information Technology"
},
{
"deptId":"3",
"deptName":"Physics"
}]
---
leftJoin(department,employee,(dept)->(dept.deptId),(emp)->(emp.deptId))
Output
xxxxxxxxxx
[
{
"l": {
"deptId": "1",
"deptName": "Science"
},
"r": {
"employeeId": 1,
"deptId": 1,
"employeeName": "James Thomas"
}
},
{
"l": {
"deptId": "2",
"deptName": "Information Technology"
}
},
{
"l": {
"deptId": "3",
"deptName": "Physics"
},
"r": {
"employeeId": 2,
"deptId": 3,
"employeeName": "James Peter"
}
},
{
"l": {
"deptId": "3",
"deptName": "Physics"
},
"r": {
"employeeId": 3,
"deptId": 3,
"employeeName": "Jackie Chen"
}
}
]
Now, you know how to implement joins with MuleSoft DataWeave.
Opinions expressed by DZone contributors are their own.
Comments